使用match,lookup,unwind之后,需要在输出中使用$ group字段形式

时间:2018-07-04 10:54:28

标签: mongodb mongoose mongodb-query aggregation-framework

enter image description here我已应用以下查询来获取结果,但在输出中未得到 total_members

    PodMembers.aggregate([
    {
        $match: { datetime: { $exists: true } }
    },
    {
        $group: {
            _id: "$pod_id",
            total_members: { $sum: 1 }
        }
    },
    { $unwind: "$total_members" },
    {
        $lookup: {
            from: "pods",
            localField: "_id",
            foreignField: "_id",
            as: "podinfo"
        }
    },
    { $unwind: "$podinfo" },
    { "$replaceRoot": { "newRoot": "$podinfo" } },
    { $addFields: { "Status": false } }
]).exec(function (err, resp) {
    if (err) console.log(err)
    else console.log(resp)
})

pods文档,例如:

    {
    "_id": "5ae46c0aff1bde1634ad8b3f",
    "admin": "595bdd5aa6ea2811d48a7802",
    "name": "Fashion Trend",
    "datetime": 1524919038771,
    "approval": false,
    "actions": "Comments",
    "description": "Fashion is about dressing according to what's fashionable. Style is more about being yourself.",
    "profilepic": "https://cdn.com/upload/Community_photos/595bdd5a6ea2811d487802_152919188491.jpg",
    "coverpic": "https://cdn.com/upload/Community_photos/595dd5aaea2811d48a7802_152499171374.jpg",
    "__v": 0
}

pod_members文档,例如:

   {
    "_id": "5b127683187a4f19901e34b7",
    "__v": 0,
    "pod_id": "5ae470d04e15d607f043a20e",
    "instaid": "5ad9a44cabd2180d64073462",
    "username": "out_tur",
    "user_id": "595bdd5aa6ea2811d48a7802",
    "member_name": "Sagar Dalal",
    "req_date": 1527936643123,
    "datetime": 1527938142745,
}

我还附加了我从上述代码中获得的输出:

0 个答案:

没有答案