如何使用 Monogodb 中的字段名称计算产品组? [英] How to Count Product group wise with Field name in Monogodb?

查看:34
本文介绍了如何使用 Monogodb 中的字段名称计算产品组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天,我有这样的收藏,

Good day, I have this collections like below,

| 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 is mongodb object Id for hub user.

所以我想通过 hubId 对 productStatus 进行分组,就像下面的 JSON 格式

So I would like to group productStatus by hubId Like below in JSON format

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

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

any body help me how to query for such result ?

并通过 hubID 填充 hub 信息.我做到了

and populate hub information by hubID. I did it

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

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

But i am new to generate group data in mongodb

推荐答案

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

You can leverage the use of $arrayToObject operator within a series of pipelines and a final $replaceRoot pipeline to get the desired result.

注意:hubId 在管道操作过程中被转换为字符串作为 $arrayToObject 操作符如果key"值是一个字符串.因此,如果 hubId 是 ObjectId,则 $ 时需要 $toString应用了arrayToObject.

Note: hubId is converted to a string during the pipeline operation as $arrayToObject operator works well if the "key" value is a string. So if hubId is an ObjectId then $toString is needed when $arrayToObject is applied.

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

You would need to run the following aggregate pipeline:

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
    }
}

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

This of course does not produce the other status that has zero count but the solution can be a good starting point.

这篇关于如何使用 Monogodb 中的字段名称计算产品组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆