16

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?

Evaldas Ilginis
  • 502
  • 4
  • 9

1 Answers1

30

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
Serkan Durusoy
  • 5,447
  • 2
  • 20
  • 39