目的是找到满足一定条件的house被不通用户关注的用户列表
house-user:
{
"_id" : ObjectId("5b17a412882b7f075496b2b6"),
"house_id" : 1.0,
"user_id" : 2.0
}
{
"_id" : ObjectId("5b17a424882b7f075496b2b7"),
"house_id" : 2.0,
"user_id" : 2.0
}
{
"_id" : ObjectId("5b17a42f882b7f075496b2b8"),
"house_id" : 2.0,
"user_id" : 5.0
}
house:
{
"_id" : ObjectId("5b17a2e1c82389528e424f4e"),
"house_id" : 1.0,
"house_name" : "house1",
"city_id" : 1.0
}
{
"_id" : ObjectId("5b17a309882b7f075496b2ae"),
"house_id" : 2.0,
"house_name" : "house2",
"city_id" : 1.0
}
{
"_id" : ObjectId("5b17a32b882b7f075496b2af"),
"house_id" : 3.0,
"house_name" : "house3",
"city_id" : 1.0
}
aggregation script:
// Requires official MongoShell 3.6+
use mydb;
db.getCollection("test_house").aggregate(
[
{
"$match" : {
"city_id" : 1.0
}
},
{
"$lookup" : {
"from" : "house_user",
"localField" : "house_id",
"foreignField" : "house_id",
"as" : "userInfo"
}
},
{
"$unwind" : {
"path" : "$userInfo"
}
},
{
"$group" : {
"_id" : "$userInfo.user_id"
}
}
{ // 例子而已
"$project":{
"_id":0,
"user_id":1
}
}
],
{
"allowDiskUse" : false
}
);
网友评论