1

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] }
1
  • i need to solve the exact same problem...were you able to find a solution? Commented Jun 16, 2015 at 0:17

1 Answer 1

0

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 ] }
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.