Shalu
Shalu

Reputation: 251

How to find within a embedded document of Mongodb a key if it?

{
   _id: "joe",
   name: "Joe Bookreader",
   addresses: [
                {
                  street: "123 Fake Street",
                  city: "Faketon",
                  state: "MA",
                  zip: "12345"
                },
                {
                  street: "1 Some Other Street",
                  city: "Boston",
                  state: "MA",
                  zip: "12345"
                }
              ]
 }

How to find the key street if it exists in this document?

Upvotes: 1

Views: 1844

Answers (1)

Artem Petrosian
Artem Petrosian

Reputation: 2954

You can use $exists operator:

db.yourCollectionName.find( { 'addresses.street': { $exists: true } } );

Upvotes: 4

Related Questions