2

I am new with MongoDB. I want to retrieve document based on condition on nested document. Example :

db.user.insert([
{name:"abc",uid:"abc.b",pass:"abc3",follows:500,posts:78},
{name:"xyz",uid:"xyz.r",pass:"xyz4",follows:600,posts:78},
{name:"pqr",uid:"pqr.s",pass:"pqr5",follows:600,posts:78,
      comments:[{
                  uid:"abc.b",msg:"great"},{uid:"xyz.r",msg:"awesome"}
               ]}
])

Here, how to retrieve documents based upon condition equivalent to RDBMS as :

 select * where comments.user="abc";

By which I can get users commented by "abc".

Thanks in Advance.

2 Answers 2

2

Use the dot notation to access the embedded array element in your query object. For example, the following will query for documents in the user collection on the uid field of the comments array with value "abc.d":

db.user.find({"comments.uid": "abc.b"});
Sign up to request clarification or add additional context in comments.

Comments

-1
db.user.find({"comments.user"="abc"})

1 Comment

This one is wrong though. '=' has nothing to do with conditional queries in MongoDB

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.