然后填充然后在猫鼬中聚集

时间:2016-08-12 14:42:00

标签: javascript node.js mongodb mongoose aggregation-framework

我真的想填充然后汇总

    Comps.find().populate({
        path : "reviews",
        model : "Review"
    }).exec(function(err, populatedData){
        if(err) console.log(err)
        console.log("populatedData--", populatedData)
    }).aggregate([
        {$unwind : "$reviews"}
    ]).exec(function(err, doc){
        if(err) throw err;
        console.log("statements from aggregate", doc)
    })

当用户进行审核时,我会将评论的对象ID添加到Comps集合的reviews数组中。现在我填写了Comps中的评论。我想在评论中做一些总结。

我在this question看到我因为客户端和服务器端的东西而无法做到,但我不知道如何修复它。

在链接中,答案基本上是制作自己的populate方法来整理集合?当他使用$lookup

这是> db.comps.find().pretty()

的文档之一
{
        "_id" : ObjectId("579706567f9c8cbc07482e65"),
        "name" : "comp1",
        "industry" : "industry1",
        "anonUsers" : [ ],
        "users" : [ ],
        "reviews" : [
                "57ad8c4353ef022423dcdadb",
                "57ad98e5cdc0ec7009530519",
                "57ad997e51c65ab8283d9c19",
                "57ad99f3480d0ffc141ffdf3",
                "57ad9aafcba3fb3029b22b19",
                "57ad9b953643bbb428034966",
                "57ad9d022ac828040b51f824",
                "57ad9d362bd44db8226efd47",
                "57ad9e6f000e02082adf1110",
                "57ad9fa3e4c8a91c20e8b805",
                "57ada040717ddc10250b2255",
                "57ada069e3f0f96422253364",
                "57adf85d6312904c0ee62ae5",
                "57adfce8b9be606007c073b1",
                "57adff001600b53c0fe74d35"
        ],
        "__v" : 16
}

这里有一个db.reviews.find().pretty()

{
        "_id" : ObjectId("57adff001600b53c0fe74d35"),
        "updatedAt" : ISODate("2016-08-12T16:53:30.510Z"),
        "createdAt" : ISODate("2016-08-12T16:53:20.318Z"),
        "vote" : "up",
        "reviewText" : "coolio again",
        "company" : ObjectId("579706567f9c8cbc07482e65"),
        "companyName" : "comp1",
        "userType" : "anon",
        "user" : ObjectId("57adfef51600b53c0fe74d34"),
        "statements" : [
                {
                        "name" : "statement3",
                        "question" : "This is the question for statement3",
                        "result" : 8
                },
                {
                        "name" : "statement4",
                        "question" : "This is the question for statement4",
                        "result" : 7
                },
                {
                        "name" : "statement6",
                        "question" : "This is the question for statement6",
                        "result" : 6
                }
        ],
        "__v" : 1,
        "className" : "thisUser",
        "momented" : "a few seconds ago"
}

修改 我尝试从链接中执行此操作。它只是给我comps和评论是一个id列表。 reviews应该是一个对象数组。对象应该是reviews集合中的评论。喜欢人口

Comps.aggregate([
    {"$lookup": {
        "from" : "Reviews",
        "localField" : "_id",
        "foreignField": "_id", 
        "as": "returndComp"
    }}
], function(err, result){
    if(err) throw err;
    console.log("result", result)
})

1 个答案:

答案 0 :(得分:0)

如链接的答案所述,您不能populate()然后aggregate(),但您可以使用 $lookup 来实现您的需求。由于reviews是一个数组,因此您需要在管道中添加 $unwind 阶段。

最后的额外 $unwind 阶段是从 $lookup 管道中展平数组字段。

以下示例显示了这一点:

Comps.aggregate([
    { "$unwind": "$reviews" },
    {
        "$lookup": {
            "from": "Reviews",
            "localField": "reviews",
            "foreignField": "_id",
            "as": "review"
        }
    },
    { "$unwind": "$review" },
]).exec(function(err, doc){
    if(err) throw err;
    console.log("statements from aggregate", doc)
});