我正在使用总合数据对MongoDB的询问数据,但我有一组问题,我想把多美元小组阶段合并为一美元项目,最后看上去似乎与我所期望的一样。 在下面的法典中,我只字不提:<条码>。 我如何解决这一问题?
这里是我的总管道:
const user = userCollection.aggregate([
{
$match: {
_id: ObjectId(id)
}
},
// lookup images list
{
$lookup: {...}
},
{
$unwind: {
path: $images ,
preserveNullAndEmptyArrays: true,
},
},
{ $sort: { images.createdAt : -1 } },
{
$group: {
_id: $_id ,
images: {
$push: $images ,
},
},
},
// lookup blocked list
{
$lookup: {...}
},
{
$unwind: {
path: $blocked ,
preserveNullAndEmptyArrays: true,
},
},
{ $sort: { blocked.createdAt : -1 } },
{
$group: {
_id: $_id ,
blocked: {
$push: $blocked ,
},
},
},
// lookup followers list
{
$lookup: {...}
},
{
$unwind: {
path: $followers ,
preserveNullAndEmptyArrays: true,
},
},
{ $sort: { followers.createdAt : -1 } },
{
$group: {
_id: $_id ,
followers: {
$push: $followers ,
},
},
},
{
$project: {
_id: 1,
name: 1,
age: 1,
bio: 1,
images: 1,
blocked: 1,
followers: 1,
}
}
]);
console.log(user);
// Results: [{ _id: ... }, { _id: ... }, { _id: ... }, { _id: ... }]