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?
Asked
Active
Viewed 6,784 times
1 Answers
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
-
1Thank you very much. It really works. Only _searchKeywords_ mustn't be put into the curly braces because $in demands an array. – Evaldas Ilginis Dec 12 '15 at 22:11
-
Aoff, yes, fixed that! Thanks – Serkan Durusoy Dec 13 '15 at 07:10
-
Here is a link to the relevant documentation: https://docs.mongodb.com/manual/reference/operator/query/in/#use-the--in-operator-to-match-values-in-an-array – Linus Unnebäck Mar 02 '22 at 16:37