Suppose you have a MongoDB collection and need to find documents where a certain field contains more than N elements. There are a few simple ways to handle this.
The most straightforward approach is to use the where is JavaScript, so it runs noticeably slower.
Another neat method is:
JavaScript
db.accommodations.find({'name.1': {$exists: true}})This finds the document where there exists an item in name whose id is 1.
In theory, you could also use this approach for greater than, greater than or equal to, less than or equal to, equal to, etc. — you just need to combine conditions with $or. But the query gets more complicated, and it may not be any faster than the previous approach.
ref:
Comments
No comments yet — be the first to write one.