user3282341
user3282341

Reputation: 1

Searching a collection on parts of values

Suppose I have a collection C with items with a property X. Suppose the values of X are themselves objects, list {a:1, b: 2, c: 3}. Can I find (or findOne) on C asking for items whose X property has a value whose a property == 1? I'd like to write C.find({X.a: 1}). Or maybe

C.find({X: function(value) { 
              return value.a == 1; 
           }
       });   

Upvotes: 0

Views: 47

Answers (2)

user728291
user728291

Reputation: 4138

Your pseudo code just needs quotes around the property for mongo to understand it. C.find({'X.a': 1}) would return any document where X.a equalled 1.

The key words if you want to learn more are 'subdocuments' and 'dot notation' as described here.

Upvotes: 1

vmr
vmr

Reputation: 1935

You can use dot notation to access the elements in nested documents but you will need to do $unwind if you need to access elements within a list and then check if X.a equals 1.

Upvotes: 0

Related Questions