如何使用monodb中的聚合从数组中删除空值?

时间:2016-03-31 05:33:37

标签: javascript angularjs node.js mongodb mongoose

查询:

{ "$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 ]
                        }
                    }
                }
            }

实际上在$ c​​ond的情况下聚合查询情况我正在用'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
      }
    ]
}
]

0 个答案:

没有答案