简体   繁体   English

MongoDB 如何使用 $match 查找多个文档

[英]MongoDB how to find multiple documents using $match

I have a collection like this我有这样的收藏

{
 "name": "Sai Darshan"
}
{
 "name": "Sathya"
}
{
 "name": "Richie"
}

I want to match the documents with the name "Sathya" and "Richie".我想匹配名称为“Sathya”和“Richie”的文件。

How can I achieve this using $match .我怎样才能使用$match来实现这一点。 I currently tried this我目前试过这个

$db = $this->dbMongo->selectDB("userData");
$collection = $db->selectCollection("userObject");

$aggregationFields = [
      [
        '$match' => [
          'name'=> 'Sathya',
          'name'=> 'Richie',
        ]
      ]
 ];

$cursor = $collection->aggregate($aggregationFields)->toArray();

Currently I am getting only the document目前我只得到文件

{
 "name": "Richie"
}

I am expecting to fetch both documents ie the documents with the name "Sathya" and "Richie".我期待获取这两个文件,即名为“Sathya”和“Richie”的文件。 I expect to do this with $match itself because I have further pipelines I want to pass this data to.我希望使用 $match 本身来做到这一点,因为我有更多的管道要传递这些数据。

Is there anyway I can achieve this?.无论如何我可以做到这一点吗? Any help is appreciated.任何帮助表示赞赏。

Thank you.谢谢你。

@nimrod serok answered in the comments , which is to use the $in operator . @nimrod serok 在评论中回答,即使用$in运算符

What is probably happening in with the query in the description is that the driver is de-duplicating the name entry.描述中的查询可能发生的情况是驱动程序正在对name条目进行重复数据删除。 So the query that the database receives only includes the filter for 'name'=> 'Richie' .因此,数据库收到的查询仅包含'name'=> 'Richie'的过滤器。 You can see some reference to that here in the documentation , and javascript itself will also demonstrate this behavior:您可以在文档中看到对这里的一些引用,并且 javascript 本身也将演示此行为:

> filter = { name: 'Sathya', name: 'Richie' };
{ name: 'Richie' }
>

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

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