Mongodb 尝试从聚合中返回选定的字段

2024-04-09

我的聚合函数遇到问题。我试图从数据库中获取用户最常见的订单,但我只返回名称和计数。我尝试过使用$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
   }

]

Thanks!


这里主要讲的是,$project http://docs.mongodb.org/manual/reference/operator/aggregation/project/依赖于“右侧”文档中字段属性的“绝对路径”。快捷方式如1仅适用于该元素实际上是文档顶层的位置。

此外,当您$group http://docs.mongodb.org/manual/reference/operator/aggregation/group/,所以这就是您使用各种分组运算符的地方,例如$first http://docs.mongodb.org/manual/reference/operator/aggregation/first/ and $addToSet http://docs.mongodb.org/manual/reference/operator/aggregation/addToSet/ or $push http://docs.mongodb.org/manual/reference/operator/aggregation/push/保留您从内部数组中提取的信息。而且你必须$unwind http://docs.mongodb.org/manual/reference/operator/aggregation/unwind/此处也两次,因为您要跨文档组合“类型”,并且您不希望只需要$first http://docs.mongodb.org/manual/reference/operator/aggregation/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) {

});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Mongodb 尝试从聚合中返回选定的字段 的相关文章

随机推荐