I have a collection like
{ arr:[1] }
{ arr:[1,2] }
{ arr:[1,2,3] }
and I have a search array.
[1,2]
so I want to find all the documents that are subsets of the search array or match it.
the query here should return
{ arr:[1] }
{ arr:[1,2] }
I have a collection like
{ arr:[1] }
{ arr:[1,2] }
{ arr:[1,2,3] }
and I have a search array.
[1,2]
so I want to find all the documents that are subsets of the search array or match it.
the query here should return
{ arr:[1] }
{ arr:[1,2] }
Exactly matching: db.collection.find({ $or: [ {arr: [1,2] }, { arr:[1]} ] })
If you add additional field which count amount of arr's elements, like that:
{ arr:[1], cnt: 1 }
{ arr:[1,2], cnt: 2 }
{ arr:[1,2,3], cnt: 3 }
you may use this query: db.collection.find({ arr: { $in: [1,2] }, cnt: {$in:[1,2]} }).
Next example in the last case: db.collection.find({ arr: { $in: [1,2,3] }, cnt: {$in:[2,3]} }) will output:
{ "cnt" : 2, "arr" : [ 1, 2 ] }
{ "cnt" : 3, "arr" : [ 1, 2, 3 ] }