MongoDb filter if the array of fields and the array of arguments intersect

I am creating a Meteor training project. It has a collection, in its documents there is a property called keywords, which is an array of strings. I have a second array of strings. I want to filter the collection so that it returns only those documents that the keywords intersect with this second array, i.e. Both arrays have one or more identical elements. Is it possible?

+5
source share
1 answer

Yes, the query will look like this:

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 
+11
source

Source: https://habr.com/ru/post/1237995/


All Articles