如何使用PHP在MongoDB中选择具有特定元素的文档


How to select documents with specific elements in MongoDB with PHP?

我有一个mongo文档的以下结构:

{
 "_id": ObjectId("4fba2558a0787e53320027eb"),
 "replies": {
    "0": {
      "email": ObjectId("4fb89a181b3129fe2d000000"),
      "sentDate": "2012-05-21T11: 22: 01.418Z",
      "type": "one"
    } 
    "1": {
     "email": ObjectId("4fb89a181b3129fe2d000000"),
     "sentDate": "2012-05-21T11: 22: 01.418Z",
      "type": "two"
    } 
    "2" ....
 }
}

如何仅选择仅包含特定类型答复的文档,例如"类型":"一"?谢谢!

它只是像这样工作,第一次我试图在上面做一个小组:

$cursor = $db->foo->find(
    array('replies.type' => 'one'), array('replies.sentDate')
);

也许它会帮助像我这样的人。键"0","1"...没有效果。

在 mongo 文档中查找$elemMatch,我认为它会有所帮助。http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-%24all

你可以试试这个:

t.find({replies: {$elemMatch: { "type": "one" } } });