Mongodb: find array of hashes

db.test.insert ({_ ID: 1, link: [{type: 'SMS'}]})

db.test.find () {"_id": 1, "communication": [{"type": "sms"}]}

Ok, its inserted

db.test.find ({'' communication: {type: 'SMS'}}) {"_id": 1, "communication": [{"type": "sms"}]}

Ok I can find it if its an exact match

db.test.update ({_ id: 1}, {link: [{type: 'sms', call_id: 9878}]}

Now I have updated it so that the hash nested in the array has two keys

.test.update ({_ id: 1}, {links: [{type: 'sms', call_id: 9878}}}

But I can't find it bc hash is not an exact match! Nooo!

db.test.find ({'' connections: {type: 'SMS'}}). Count () => 0

So how can I do a search like this where I want to match one of the keys in a hash in an array?

+3


source to share


1 answer


If I got it right (I have no guarantees!), Then I think what you are looking for is dot notation.

db.test.find({'communications.type':'sms'}).count()

      



Here's a link to MongoDb.org with all examples.

+2


source







All Articles