【问题标题】:Mongodb Trying to get selected fields to return from aggregateMongodb试图让选定的字段从聚合中返回
【发布时间】:2014-08-25 15:44:56
【问题描述】:

我的聚合函数有问题。我试图从数据库中获取用户最常见的订单,但我只返回名称和数量。我已经尝试使用 $project 运算符,但我似乎无法让它返回除 $group 语句中的内容之外的任何内容。

这是我当前的聚合函数:

OrderModel.aggregate(
        {$unwind: "$products"},
        {$match: { customerID: customerID }},
        {$group: { _id: "$products.name", count: {$sum:1}}},
        {$project: {name: "$_id", _id:0, count:1, active:1}},
        {$sort: {"count" : -1}},
        {$limit: 25 })

这只是产生如下输出{"count":10, "name": foo"} 而我想返回整个对象;嵌入式文档和所有。有什么想法我哪里出错了吗?

编辑 - 添加示例文档和预期输出

文档:

{
    "charge": {},
        "captured": true,
        "refunds": [
        ],
        "balance_transaction": "txn_104Ics4QFdqlbCVHAdV1G2Hb",
        "failure_message": null,
        "failure_code": null,
        "amount_refunded": 0,
        "customer": "cus_4IZMPAIkEdiiW0",
        "invoice": null,
        "dispute": null,
        "statement_description": null,
        "receipt_email": null
    },
    "total": 13.2,
    "userToken": "cus_4IZMPAIkEdiiW0",
    "customerID": "10152430176375255",
    "_id": "53ad927ff0cb43215821c649",
    "__v": 0,
    "updated": 20140701082928810,
    "created": 20140627154919216,
    "messageReceived": false,
    "ready": true,
    "active": false,
    "currency": "GBP",
    "products": [
        {
            "name": "Foo",
            "active": true,
            "types": [
                {
                    "variants": [
                        {
                            "name": "Bar",
                            "isDefault": false,
                            "price": 13.2
                        }
                    ]
                }
            ]
        }
    ]
}

预期结果:

[
    {
        "name": "Foo",
        "active": true,
        "types": [
            {
                "variants": [
                    {
                        "name": "Bar",
                        "isDefault": false
                    }
                ]
            },
            {
                "variants": [
                    {
                        "name": "Something else",
                        "isDefault": false
                    }
                ]
            }
        ],
        "quantity": 10
   },
   {
       "name": "Another product",
       "active": true,
       "types": [
           {
               "variants": [
                   {
                       "name": "Bar",
                       "isDefault": false
                   }
               ]
           }
       ],
       "quantity": 7
   }

]

谢谢!

【问题讨论】:

  • 显示您收藏的示例文档。但是,如果“customerID”实际上是跨文档的分组,那么如果您想要返回原始文档,那么您正在做的事情将不起作用。显示源文档和您的预期结果。
  • @NeilLunn 我现在更新了帖子。 CustomerId 不是跨文档的分组,只是此文档中的一个字段。

标签: node.js mongodb mongoose aggregation-framework


【解决方案1】:

这里大致说来,$project 依赖于“右侧”文档中字段属性的“绝对路径”。诸如1 之类的快捷方式仅适用于该元素实际上是文档顶层的位置。

您还需要在$group 时保留字段,因此您可以在此处使用各种分组运算符,例如$first$addToSet$push,以保留您从内部提取的信息大批。而且您还必须在此处使用两次 $unwind,因为您正在跨文档组合“类型”,并且在这种情况下您不希望只有 $first

OrderModel.aggregate([
    { "$unwind": "$products" },
    { "$unwind": "$products.types" },
    { "$group": {
        "_id": "$products.name",
        "active": { "$first": "$products.active" },
        "types": { "$addToSet": "$products.types" },
        "quantity": { "$sum": 1 }
    }},
    { "$project": {
        "_id": 0,
        "name": "$_id",
        "active": 1,
        "types": 1,
        "quantity": 1
    }}
],function(err,results) {

});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-20
    • 1970-01-01
    • 2015-07-23
    • 2021-06-29
    • 2017-10-27
    相关资源
    最近更新 更多