0

So I have these mongo fields

    "username" : "aaaaaa",
    "emails" : [
               {
                   "address" : "[email protected]",
                   "verified" : false
               }
               ],
    "profile" : {
                   "firstname" : "a",
                   "lastname" : "a"
                }

I can do

db.users.find({username:'aaaaaa'});

My question is, how about if I want to use the email address or profile's firstname?

It seems like this

db.users.find({email:{firstname:'[email protected]'}});

but it doesn't work. and I can't use

email['address']:'[email protected]'

or even

profile:{firstname:'a'} // profile.firstname:'a';

How can I get only their value?

1
  • 3
    Try db.users.find({'email.firstname':'[email protected]'}); Commented Dec 26, 2013 at 10:55

1 Answer 1

3

If you want to use the email address to query then,

db.users.find({'emails.address':'[email protected]'});

If you want to use the profile's firstname to query,

db.users.find({'profile.firstname':'a'});
Sign up to request clarification or add additional context in comments.

1 Comment

Actually, you'll want find({'emails.0.address': 'foo'}); -- note the 0 between emails and address. This is because emails is an array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.