0

I have a Document setup like this:

Users

{
 _id : '',
 name : '',
 friends : [
   {
     'name' : '',
     'email' : '',
     'age' : ''
   }
 ]
}

I'm trying to select a single friend from within the friends array by email, while only returning that specific object.

Any suggestions how to do this?

Thanks!

1 Answer 1

2
db.mytest.find({'friends.email':'abhi2'},{friends : {$elemMatch : {email : 'abhi2'}}}).pretty() 
Or
db.mytest.find({'_id':'a'},{friends : {$elemMatch : {name : 'abhi2'}}}).pretty()

For more details, look this doc : http://docs.mongodb.org/manual/reference/projection/elemMatch/

Sign up to request clarification or add additional context in comments.

Comments

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.