【问题标题】:Compute Percentage and Count document using MongoDB Query使用 MongoDB 查询计算百分比和计数文档
【发布时间】:2021-08-31 05:47:21
【问题描述】:

我正在尝试创建一个查询,它计算总金额、计算交易数量以及计算总金额的百分比。下面提供了示例文档和预期的输出。

示例文档

[
  {
    "_id": ObjectId("60dc27ac54b7c46bfa1b84b4"),
    "orders": [
      {
        "_id": ObjectId("60dc27ac54b7c46bfa1b84be"),
        "amount": 2000
      },
      {
        "_id": ObjectId("606557900dd9a34bcc0faf90"),
        "amount": 1500
      }
    ],
    "orderdate": "2021-08-29T00:00:00.000Z",
    "orderarea": "East Branch"
  },
  {
    "_id": ObjectId("60dc27ac54b7c46bfa1b75ge2"),
    "orders": [
      {
        "_id": ObjectId("6068016a7feeb34a6013e687"),
        "amount": 3000
      },
      {
        "_id": ObjectId("60dca0ea4550fd32beb5ad38"),
        "amount": 5000
      }
    ],
    "orderdate": "2021-08-30T00:00:00.000Z",
    "orderarea": "West Branch"
  },
  {
    "_id": ObjectId("60dc9ecf188cf96dd6f58c45"),
    "orders": [
      {
        "_id": ObjectId("60dca0ea4550fd32beb5ad3a"),
        "amount": 4500
      },
      {
        "_id": ObjectId("60dcac6d51b2bd1e009d7bdc"),
        "amount": 6000
      }
    ],
    "orderdate": "2021-08-31T00:00:00.000Z",
    "orderarea": "East Branch"
  }
]

输出

{"East Branch": 14000, "countoftransaction": 4, "amountpercentage": 63.63%},
{"West Branch": 8000, "countoftransaction": 2, "amountpercentage": 36.36%}

百分比应该是从东支和西支的总和22,000中计算出的百分比。

谢谢!

【问题讨论】:

    标签: mongodb mongodb-query


    【解决方案1】:

    类似的解决方案可以是:

    • 首先 $unwind orders 解构数组。
    • 使用$facet 它将计算“两种方式”。
    • 以一种方式$group 获取每个区域的总价值
    • 以其他方式$group 获取所有金额。
    • 然后得到第一个位置($facet输出一个数组)和$unwind再次。
    • 最后阶段是获得最终表示。我添加了$concat 以使用% 将百分比输出为字符串
    db.collection.aggregate({
      "$unwind": "$orders"
    },
    {
      "$facet": {
        "data": [
          {
            "$group": {
              "_id": "$orderarea",
              "total": {
                "$sum": "$orders.amount"
              },
              "countoftransaction": {
                "$sum": 1
              }
            }
          }
        ],
        "totalAmount": [
          {
            "$group": {
              "_id": null,
              "totalAmount": {
                "$sum": "$orders.amount"
              }
            }
          }
        ]
      }
    },
    {
      "$project": {
        "data": 1,
        "totalAmount": {
          "$arrayElemAt": [
            "$totalAmount",
            0
          ]
        }
      }
    },
    {
      "$unwind": "$data"
    },
    {
      "$set": {
        "totalAmount": "$totalAmount.totalAmount"
      }
    },
    {
      "$project": {
        "id": "$data._id",
        "countoftransaction": 1,
        "total": "$data.total",
        "amountpercentage": {
          "$concat": [
            {
              "$toString": {
                "$round": [
                  {
                    "$multiply": [
                      {
                        "$divide": [
                          "$data.total",
                          "$totalAmount"
                        ]
                      },
                      100
                    ]
                  },
                  2
                ]
              }
            },
            "%"
          ]
        }
      }
    })
    

    例如here

    注意最后的结果是这样的:

    [
      {
        "amountpercentage": "36.36%",
        "id": "West Branch",
        "total": 8000
      },
      {
        "amountpercentage": "63.64%",
        "id": "East Branch",
        "total": 14000
      }
    ]
    

    没有一种简单的方法可以将值作为键。此外,如果您愿意,可以使用this example,其输出结果为:

    [
      {
        "East Branch": {
          "amountpercentage": "63.64%",
          "countoftransaction": 4,
          "total": 14000
        }
      },
      {
        "West Branch": {
          "amountpercentage": "36.36%",
          "countoftransaction": 2,
          "total": 8000
        }
      }
    ]
    

    【讨论】:

    • 一个额外的问题,如果每个我都会放一个“countpercentage”,我可以将它包含在方面的 TotalAmount 中或在那里创建另一个计算吗?
    【解决方案2】:

    使用此聚合,但在 mongo 中我们无法创建动态字段名称 "East Branch": 14000

    这是聚合

    [
        {
            '$unwind': {
                'path': '$orders'
            }
        }, {
            '$group': {
                '_id': None, 
                'total': {
                    '$sum': '$orders.amount'
                }, 
                'orig': {
                    '$push': '$$ROOT'
                }
            }
        }, {
            '$unwind': {
                'path': '$orig'
            }
        }, {
            '$group': {
                '_id': '$orig.orderarea', 
                'countoftransaction': {
                    '$sum': 1
                }, 
                'ff': {
                    '$sum': '$orig.orders.amount'
                }, 
                'dd': {
                    '$addToSet': '$total'
                }
            }
        }, {
            '$addFields': {
                'total': {
                    '$first': '$dd'
                }
            }
        }, {
            '$project': {
                'amountpercentage': {
                    '$round': [
                        {
                            '$multiply': [
                                {
                                    '$divide': [
                                        '$ff', '$total'
                                    ]
                                }, 100
                            ]
                        }, 2
                    ]
                }, 
                'countoftransaction': 1, 
                'amount': '$ff'
            }
        }
    ]
    

    结果是这样的:

    [
    {_id:"West Branch",countoftransaction:2,amountpercentage:36.36,amount:8000},
    {_id:"Eest Branch",countoftransaction:4,amountpercentage:63.64,amount:14000}
    ]
    

    【讨论】:

      猜你喜欢
      • 2012-05-01
      • 2017-10-17
      • 2011-09-06
      • 2022-01-23
      • 1970-01-01
      • 2014-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多