查询:
{ "$match":{ "followuser": new mongoose.Types.ObjectId(req.query.user_id),"status":1 }},
{ '$lookup':{ from: 'users',localField: 'user',foreignField: '_id', as: 'followerDetails' } },
{ '$unwind': '$followerDetails'},
{ '$match': { "followerDetails.status" :1}},
{ '$lookup':{ from: 'shops',localField: 'user',foreignField: 'shop_owner',
as: 'shopDetails' } },
{ '$unwind': '$shopDetails'},
{ '$lookup':{ from: 'follow_user',localField: 'followerDetails._id',foreignField: 'followuser',as: 'checkfollowstatus' } },
{ $unwind: { path: "$checkfollowstatus", preserveNullAndEmptyArrays: true}},
{ '$lookup':{ from: 'product',localField: 'shopDetails._id',foreignField: 'shop',as: 'productDetails' } },
{ $unwind: { path: "$productDetails", preserveNullAndEmptyArrays: true}},
{
"$group": {
_id: '$_id',
followerDetails:{$first:"$followerDetails"},shopDetails:{ $first:"$shopDetails"},
checkfollowstatus:{$first:"$checkfollowstatus"},
productDetails: {
"$addToSet": {
$cond: [{$eq: ["$productDetails.status", 1]}, "$productDetails",null ]
}
}
}
}
实际上在$ cond的情况下聚合查询情况我正在用'null'替换然后我正在推送数组我将像“productDetails”:[null],。我怎样才能使用聚合查询这样“productDetails”:[], 的结果:
[
{
"_id": null,
"count": 2,
"documentData": [
{
"_id": "56f39a0c0a6eb27f8cc40189",
"followerDetails": {},
"shopDetails": {},
"checkfollowstatus": [],
"productDetails": [
null
],
"numberOfProduct": 1
},
{
"_id": "56f399730a6eb27f8cc40188",
"followerDetails": {},
"shopDetails": {},
"checkfollowstatus": [],
"productDetails": [
null
],
"numberOfProduct": 1
}
]
}
]