简体   繁体   English

如何使用 MongoDB 聚合 $match 两个列表

[英]How to $match two lists using MongoDB aggregation

I have collection as: col1我收藏为: col1

{"_id":"5c3ea35651166f3refsfd5",
"environment": "production",
"application": ['cloud', 'android', 'finance']},

{"_id":"5c3ea35651166f3refsfd6",
"environment": "production",
"application": ['banking', 'network', 'finance']}

I want to project the data only if the application/s in collection matches with the application in the list of ['cloud', 'database', 'booking'] .只有当集合中的应用程序与['cloud', 'database', 'booking']列表中的应用程序匹配时,我才想投影数据。 In this case, I should be able to get only first document as it has ' cloud ' in both document as well as in list.在这种情况下,我应该只能获得第一个文档,因为它在两个文档和列表中都有“”。

I need to use aggregation.我需要使用聚合。 But I am not getting correct direction..但我没有得到正确的方向..

You can use $in query operator您可以使用$in查询运算符

col1.aggregate([
  { '$match': { 'application': { '$in': ['cloud', 'database', 'booking'] }}},
  { '$project': { 'environment': 1, 'application':1 }}
])

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

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