Reputation: 29
I have two collection as below First collection is Users:
{
"userid":123,
"name":"abc",
"age":20,
"status":"Active"
}
{
"userid":345
"name":"cde"
"age":25,
"status":"Active"
}
second collection is userComment:
{
"userid":123,
"commnet":"Mongodb rocks"
}
can anyone please help me writing the query to fetch the users with "Active" status alongwith a flag that will tell me whether user has any comment or not So the o/p should be
{
"userid":123,
"name":"abc",
"age":20,
"status":"Active"
"userscommentFlag":"Y"**
}
{
"userid":345
"name":"cde"
"age":25,
"status":"Active"
"userscommentFlag":"N"
}
Thanks.
Upvotes: 2
Views: 714
Reputation: 4435
Using $lookup
in aggregation pipeline this can be done as:
db.users.aggregate(
[{$lookup:
{from:"userComment", localField:"userid", foreignField: "userid", as: "comments"
}}])
Note: $lookup
is supported in mongodb 3.2
Upvotes: 1