zeedre
zeedre

Reputation: 411

mongodb : search an array of hashes

db.test.insert({_id:1,communications:[{type:'sms'}]})

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

Ok, its inserted

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

Ok, I can find it if its an exact match

db.test.update({_id:1}, {communications:[{type:'sms',call_id:9878}]}

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

.test.update({_id:1}, {communications:[{type:'sms',call_id:9878}]})

But I can't find it bc the hash is not the exact match! NOOOOO!

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

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

Upvotes: 3

Views: 3233

Answers (1)

AdaTheDev
AdaTheDev

Reputation: 147354

If I've understood correctly (no guarantees I have!), then I think what you are looking for is the dot notation.

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

Here's the reference on MongoDb.org with all the examples.

Upvotes: 2

Related Questions