简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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