Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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, i.e. both arrays have one or several same elements. Is it possible?

like image 974
Evaldas Ilginis Avatar asked Dec 12 '15 20:12

Evaldas Ilginis


1 Answers

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
like image 185
Serkan Durusoy Avatar answered Sep 29 '22 07:09

Serkan Durusoy