【问题标题】:How to Count Product group wise with Field name in Monogodb?如何在 Mongodb 中使用字段名称计算产品组?
【发布时间】:2021-01-20 21:29:38
【问题描述】:

美好的一天,我有这个收藏,如下所示,

| hubId | ProductStatus | CreatedAt | 
|:---- |:------:| -----:| ---------:|
| xyz  | On the Way    | 2021-01-01
| mlm  | Delivered    |  2021-01-01
| yyy  | Delivered    |  2021-01-01
| xyz  | Delivered    |  2021-01-01
| yyy  | Cancelled    |  2021-01-01
| yyy  | On the Way    | 2021-01-01
| mlm  | On the Way    | 2021-01-01
| mlm  | Delivered    | 2021-01-15

hubId 是 hub 用户的 mongodb 对象 ID。

所以我想按 hubId 对 productStatus 进行分组,如下所示,采用 JSON 格式

{
    "xyz" : {
        "Delivered" : 1,
        "On the Way" : 1
    },
    "mlm" : {
        "On the Way" : 1,
        "Delivered" : 2
    },
    "yyy" : {
        "On the Way" : 1,
        "Delivered" : 1,
        "Cancelled" : 1
    }
}

任何机构帮助我如何查询这样的结果?

并通过 hubID 填充集线器信息。 我做到了

Product.aggregate({
      $group: {
        _id: "$hub",
        hub: { $addToSet: "$hub" },
        productStatus: { $addToSet: "$productStatus" },
        count: { $sum: 1 },
      },
    });

但我是新来在 mongodb 中生成组数据

【问题讨论】:

    标签: mongodb mongodb-query


    【解决方案1】:

    您可以在一系列管道和最终的$replaceRoot 管道中利用$arrayToObject 运算符来获得所需的结果。

    注意hubId 在管道操作期间转换为字符串,因为如果“key”值是字符串,$arrayToObject 运算符可以正常工作。因此,如果 hubId 是 ObjectId,则在应用 $arrayToObject 时需要 $toString

    您需要运行以下聚合管道:

    Product.aggregate([
        {  "$group": {
                "_id": {
                    "hubId": "$hubId",
                    "status": "$ProductStatus"
                },
                "count": { "$sum": 1 }
        } },
        { "$group": {
            "_id": "$_id.hubId",
            "counts": {
                "$push": {
                    "k": "$_id.status",
                    "v": "$count"
                }
            }
        } },
        { "$group": {
            "_id": null,
            "counts": {
                "$push": {
                    "k": { "$toString": "$_id" },
                    "v": "$counts"
                }
            }
        } },
        { "$addFields": {
            "counts": {
                "$map": {
                    "input": "$counts",
                    "in": {
                        "$mergeObjects": [
                            "$$this",
                            { "v":  { "$arrayToObject": "$$this.v" } }
                        ]
                    }
                }
            }
            
        } },
        {  "$replaceRoot": {
            "newRoot": { "$arrayToObject": "$counts" }
        } }  
    ])
    

    产生以下结果文档:

    {
        "xyz" : {
            "Delivered" : 1,
            "On the Way" : 1
        },
        "mlm" : {
            "On the Way" : 1,
            "Delivered" : 2
        },
        "yyy" : {
            "On the Way" : 1,
            "Delivered" : 1,
            "Cancelled" : 1
        }
    }
    

    这当然不会产生计数为零的其他状态,但解决方案可能是一个很好的起点。

    【讨论】:

    • 试试上面编辑的解决方案,看看它是否有意义。此外,上述结果是否是您所期望的结果,即 Id 的哈希值和相应的状态计数?如果没有,您能否更新您的问题以包含所需的 JSON 结果文档,谢谢!
    • 结果已更新,但在执行您的代码后,我收到 MongoError: Unrecognized expression '$toString'
    • 您的 MongoDB 版本可能不兼容,$toString 在 MongoDB 4.0 及更高版本中可用
    • 你好 Chridam 是的,它现在工作正常,谢谢,我已经更新了我的 mongodb 并且它工作正常,你能告诉我如何从 hubprofile 填充这个 hubid 吗?
    • 因为您的帖子中基本上有多个问题,而我设法回答了主要问题,请打开一个新问题,详细说明您到目前为止所拥有的内容以及您预期的 JSON 结果。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-23
    • 2014-10-15
    • 1970-01-01
    相关资源
    最近更新 更多