There are ways to structure your query to do this, the approaches are different depending on your mongodb server version. So following on from your code:
For MongoDB version 2.6 and above, use the $all
operator:
model.find({
"bars": { "$all": barsToFind }
},
And though the operator does exist for previous versions, it behaves differently so you actually need to generate an $and
statement to explicitly match each entry:
var andBars = [];
barsToFind.forEach(function(bar) {
andBars.push({ "bars": bar })
});
model.find({
"$and": andBars
},
Both ensure that you only match documents that contain all of the entries in the array as you have specified, it's just that the syntax available to MongoDB 2.6 is a little nicer.