简体   繁体   English

MongoDb 查询两个集合

[英]MongoDb query on two collections

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 :第二个集合是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任何人都可以帮助我编写查询以获取具有“活动”状态的用户以及一个标志,该标志将告诉我用户是否有任何评论所以o / p应该是

{
"userid":123,
"name":"abc",
"age":20,
"status":"Active"
"userscommentFlag":"Y"**
}
{
"userid":345
"name":"cde"
"age":25,
"status":"Active"
"userscommentFlag":"N"
}

Thanks.谢谢。

Using $lookup in aggregation pipeline this can be done as:在聚合管道中使用$lookup可以这样做:

db.users.aggregate(
[{$lookup:
 {from:"userComment", localField:"userid", foreignField: "userid", as: "comments"
}}])

Note: $lookup is supported in mongodb 3.2注意:mongodb 3.2 支持$lookup

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM