【问题标题】:populate then aggreagate in mongoose填充然后聚合在猫鼬中
【发布时间】:2016-12-19 15:04:00
【问题描述】:

我真的很想填充然后聚合

    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 看到我不能这样做,因为客户端和服务器端的东西,但我不知道如何解决它。

在链接中,答案基本上是制作自己的填充方法来组合集合?当他使用$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 集合的评论。喜欢人口

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

【问题讨论】:

  • 您能否提供一些输入文档的示例?
  • 我在问题中添加了一些内容。希望有帮助

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


【解决方案1】:

正如链接的答案所述,您不能先使用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)
});

【讨论】:

  • console.log("statements from aggregate", doc) 给了我一个空数组.. statements from aggregate [] .. 谢谢你的帮助。
  • 我已将$lookup 运算符的from 键的设置从"from": "reviews", 更改为"from": "Reviews",,因为这是您在aggregate() attemtpt 中作为基础集合名称编写的内容.试试看,看看效果如何。
  • 集合名称是 reviews 没有集合称为 Reviews 我假设您将其更改为该名称,因为您在问题中看到了我的编辑。我认为应该是reviews。我得到相同的空数组。
  • TLDR 基本上公司的评论数量与 comps 集合中的评论数组中的评论之间存在差异......我不知道这是否会有所不同,但例如对于comp1,我有很多评论(因此在评论集中制作了很多文档)。然后我实现了,当有人为 comp1 输入评论时,将该评论的 id 发送到数组,comps 集合中的 comp1 中的 reviews。在我没有这样做之前。只是说。
  • 我想我让它工作了。我前段时间设置了架构,但我忘记了我将评论元素的类型设置为String 现在我做了reviews : [{type: mongoose.Schema.Types.ObjectId, ref : "Review"}],,看起来它可以工作。谢谢。
猜你喜欢
  • 2019-08-29
  • 2017-01-23
  • 2015-12-23
  • 2015-09-15
  • 2018-01-30
  • 2015-09-30
  • 2012-11-11
  • 2016-05-09
  • 1970-01-01
相关资源
最近更新 更多