Fred J.
Fred J.

Reputation: 6029

find document where item of array equals to a string

This Meteor mongo command tries to get the document with the emails[0].address == abc@xyz but it is failing, Any ideas? thx

db.users.findOne({'emails[0].address':'[email protected]'}).pretty()

"emails" : [
        {
            "address" : "[email protected]",

Upvotes: 0

Views: 67

Answers (2)

zim
zim

Reputation: 2386

that will work only if the string you're looking for is the first element of the array. to search through the whole array, you need $elemMatch. e.g.

db.users.findOne({emails: {
    $elemMatch: {address: '[email protected]'}
}})

Upvotes: 0

Antonio Narkevich
Antonio Narkevich

Reputation: 4326

To make it working please change emails[0].address to emails.0.address.

Please see the docs on how to use dot notation in this situation.

Upvotes: 1

Related Questions