1

My doc has an array field Keys

Keys1 and Keys2 are two arrays

I want all the docs where Keys contains any value in Keys1 AND any value in Keys2

1 Answer 1

3

There's no great way to represent this query yet (as of 1.1.2) - if you ask on the list or file a feature request we can try to get something cooked up.

For now the best bet is probably to use an $in query to do half of the work:

db.test.find({keys: {$in: Keys1}})

You can do this in combination with a $where which can do the Keys2 part (but won't take advantage of an index - that's why it is good to do as much as possible with the regular query syntax). This would look something like this:

db.test.find({keys: {$in: Keys1}, $where: "for (i in this.keys) { for (j in Keys2) { if (this.keys[i] == Keys2[j]) return true;}} return false;"})

In latest versions of mongodb 2.6+ you can do this by $and operator.

db.test.find({$and:[{keys: {$in: Keys1}},{keys: {$in: Keys2}}]}) 
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.