0

我正在使用 mongodb 和 php 构建一个项目,所以我的数据库包含以下记录。

{
    "_id": {
        "$id": "515bb7e2a00b2add0b000001"
    },
    "user_id": "user_8888",
    "events": {
        "common test I": {
            "subject": "common test I",
            "start_date": "04/14/2013",
            "start_time": "8.00 AM",
            "end_date": "04/14/2013",
            "end_time": "10.30 AM",
            "all_day_event": "false",
            "discription": "no discription",
            "location": "STC",
            "private": "false",
            "time_zone": "IST",
            "alarm": "true",
            "alarm_threshold": "5",
            "status": "true"
        }
    }
}

所以只使用查询,我如何从记录中检索“common test I”事件。结果应该是:

{
    "subject": "common test I",
    "start_date": "04/14/2013",
    "start_time": "8.00 AM",
    "end_date": "04/14/2013",
    "end_time": "10.30 AM",
    "all_day_event": "false",
    "discription": "no discription",
    "location": "STC",
    "private": "false",
    "time_zone": "IST",
    "alarm": "true",
    "alarm_threshold": "5",
    "status": "true"
}

是否可以?

4

2 回答 2

0

db.collection.find({},{"events.common test I":1,_id:0})

于 2013-04-03T12:08:14.257 回答
0

您可以在 find 方法中指定要检索文档的哪些字段。

参见PHP.net 参考手册 MongoCollection::find

就像是

$collection->find(array(), array('events.common test I'=>true));

请注意“总是返回 _id 字段”。

虽然我建议不要在对象键/字段名称中使用空格。

于 2013-04-03T12:09:43.373 回答