我在mongodb中有2个收藏
属性集合
{
"_id": "5b45b9612b33c41c3cdcc00c",
"commId": "5b436da8859935243868edcc",
},
{
"_id": "5b5844644a866c2640dcc50b",
"commId": "5b436da8859935243868edcc",
}
社区收藏
{
"_id": ObjectId("5b4362e9859935243868edca"),
"userId": "5b4329cbc5a2582294326b3b",
}
{
"_id": ObjectId("5b436da8859935243868edcc"),
"userId": "5b432f0ac5a2582294326b3d",
}
{
"_id": ObjectId("5b5add46acf74732d81e9273"),
"userId": "5b47094a28409e26d0bf0657",
}
我想通过属性中的commId和在cimmunities中的_id加入他们
let result = await Properties.aggregate([
{
$lookup: {
from: 'communities',
localField: 'Object(commId)',
foreignField: 'ObjectId(_id)',
as: 'author'
}
},
{
$unwind: '$author',
},
{
$project: {
commId: 1,
author: {
'commId': '$author._id',
'userId': '$author.userId'
}
}
},
])
作为响应,必须只有2个属性,但它们却重复...
答案 0 :(得分:0)
响应
[ { "_id": "5b45b9612b33c41c3cdcc00c", "commId": "5b436da8859935243868edcc", "author": { "commId": "5b4362e9859935243868edca", "userId": "5b4329cbc5a2582294326b3b" } }, { "_id": "5b45b9612b33c41c3cdcc00c", "commId": "5b436da8859935243868edcc", "author": { "commId": "5b436da8859935243868edcc", "userId": "5b432f0ac5a2582294326b3d" } }, { "_id": "5b45b9612b33c41c3cdcc00c", "commId": "5b436da8859935243868edcc", "author": { "commId": "5b5add46acf74732d81e9273", "userId": "5b47094a28409e26d0bf0657" } }, { "_id": "5b5844644a866c2640dcc50b", "commId": "5b436da8859935243868edcc", "author": { "commId": "5b4362e9859935243868edca", "userId": "5b4329cbc5a2582294326b3b" } }, { "_id": "5b5844644a866c2640dcc50b", "commId": "5b436da8859935243868edcc", "author": { "commId": "5b436da8859935243868edcc", "userId": "5b432f0ac5a2582294326b3d" } }, { "_id": "5b5844644a866c2640dcc50b", "commId": "5b436da8859935243868edcc", "author": { "commId": "5b5add46acf74732d81e9273", "userId": "5b47094a28409e26d0bf0657" } } ]