簡體   English   中英

填充然后聚集在貓鼬中

[英]populate then aggreagate in mongoose

我真的很想填充然后聚合

    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內部填充評論。 我想對評論進行匯總。

我看到這個問題由於客戶端和服務器端的原因而無法解決,但我不知道如何解決。

在鏈接中,答案基本上是使用自己的填充方法來將集合放在一起? 當他使用$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"
}

編輯我嘗試從鏈接中執行此操作。 它只是給我補償,評論是ID列表。 reviews應該是一個對象數組。 對象應該是來自reviews集合的reviews 像人口

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

如鏈接的答案所述,您不能先執行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)
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM