0

Using PHP and Mongo I would like to update the users availability but cannot figure it out. How can I structure my collection to be able to reference availability groups.steve.availability?

Below is the structure of my "groups" collection:

{
"_id": {
    "$oid": "524327d536b82c7c5c842f6d"
},
"group_id": "testing",
"password": "test",
"users": [
    {
        "username": "steve",
        "availability": "null"
    },
    {
        "username": "joeb",
        "availability": "null"
    }
]

}

4

1 回答 1

1

如果您想按照您建议的方式引用它:groups.steve.availability,您需要像下面那样构建您的文档。(我不确定groups来自哪里)。

此示例将通过将用户名移动到字段 ( )users.steve.availability的子字段来为您提供。usersusers.steve

{
"_id": {
    "$oid": "524327d536b82c7c5c842f6d"
},
"group_id": "testing",
"password": "test",
"users": {
    "steve": {
        "availability": "null"
    },
    "joeb" : {
        "availability": "null"
    }
}
}

或者,您可以直接在文档上创建字段:

{
"_id": {
    "$oid": "524327d536b82c7c5c842f6d"
},
"group_id": "testing",
"password": "test",
"steve": {
        "availability": "null"
},
"joeb" : {
        "availability": "null"
}
}

这将允许你只使用steve.availability.

但是,如果您尝试进行查询,最好还是让它更像原来的样子:

"users": [
{
    "username": "steve",
    "availability": "null"
}]

因此,您可以编写如下查询:

db.groups.find({"users.username" : "steve" })
于 2013-10-02T01:48:21.137 回答