简体   繁体   English

如果字段数组和参数数组相交,则过滤MongoDb集合

[英]Filter MongoDb collection if field array and argument array intersect

I'm creating a Meteor learning project. 我正在创建一个流星学习项目。 There is a collection in it, its documents have a property named keywords which is an array of strings. 其中有一个集合,其文档具有名为关键字的属性,该属性是字符串数组。 I have a second array of strings. 我有第二个字符串数组。 I want to filter the collection that way, that it returned only those documents which keywords array intersect with that second array, ie both arrays have one or several same elements. 我想以这种方式过滤集合,它仅返回那些关键字数组与第二个数组相交的文档,即两个数组都具有一个或几个相同的元素。 Is it possible? 可能吗?

Yes, a query would be: 是的,查询将是:

var searchKeywords = ['a','b','c','d']

MyCollection = new Mongo.Collection('mycollection');

MyCollection.insert({
  keywords: ['x','y','a','b']
});
// returns some ID

MyCollection.findOne({
  keywords: { $in: searchKeywords } 
})._id
// returns the same ID

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

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