简体   繁体   English

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

[英]How to select documents with specific elements in MongoDB with PHP?

I have the following structure of a mongo document: 我具有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" ....
 }

}

How do I select only documents containing only specific type of replies, for example "type":"one"? 如何仅选择仅包含特定回复类型的文档,例如“ type”:“ one”? Thank you! 谢谢!

It simply worked like this, first time I was trying to do a group on it: 就像我第一次尝试对它进行分组时,它的工作原理是这样的:

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

Maybe it will help others like me. 也许它将帮助像我这样的其他人。 The keys "0", "1" ... have no effect. 键“ 0”,“ 1” ...无效。

Look to $elemMatch in mongo documentation I think it will help. 在mongo文档中查看$elemMatch ,我认为这会有所帮助。 http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-%24all http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-%24all

You may try this: 您可以尝试以下方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM