Java Mongo DB聚合管道 - $ sum在使用$ Undind时无法正常工作吗?

发布于 2025-02-08 21:47:43 字数 1483 浏览 2 评论 0原文

我目前正在我的一个Java脚本中使用MongoDB聚合管道。

输入数据可以简化为不同客户的订单列表。例如:

_idcustomerIdOrderId运输金额
1123A12B4货运1,装运2 30.00
2456A14HF装运340.00
2123A27JD货运420.00 20.00 20.00

_id,customerId和OrderId和orderId是字符串,货物的数量是多大的类型,并且货物是多数的实体。 - 班级的班级。现在,我想汇总此内容,并向每个客户提供他们所花费的总量和所有货物的总量。像这样的第一个客户:

{customerId:123,发货:[Shipment1,Shipment2,shipment2,shipment4],金额:50.00}

数据以JSON格式进行

[
 {
   "_id": 1,
   "customerId": 123,
   "orderId": "a12b4",
   "shipments": ["shipment1","shipment2"],
   "amount": 30
 },
 {
   "_id": 2,
   "customerId": 456,
   "orderId": "a14hf",
   "shipments": ["shipment3"],
   "amount": 40
 },
 {
   "_id": 3,
   "customerId": 123,
   "orderId": "a27jd",
   "shipments": ["shipment4"],
   "amount": 20
 }
]

,所以我做到了:

Aggregates.unwind("$shipments"),
Aggregates.group("$customerId",
     Accumulators.sum("amount","$amount"),
     Accumulators.addToSet("shipments", "$shipments")),
Aggregates.out("test_output")

这就是我的问题所在的地方:

只要我保留我的放松陈述,总和> 的用法就不会返回正确的结果。但是,当我用另一个我不必放松的领域取代货物时,它可以正常工作。

任何提示都将不胜感激。

I am currently working on a mongoDB aggregation pipeline in one of my Java scripts.

The input data can be simplified to a list of orders of different customers. Such as:

_idcustomerIdorderIdshipmentsamount
1123a12b4shipment1, shipment230.00
2456a14hfshipment340.00
2123a27jdshipment420.00

_id, customerId, and orderId are strings, the amount a number of type Long, and the shipments are entities of a custom-made class called shipment. Now, I want to aggregate this and show, for every customerId, the total amount of what they spent and all the shipments. Like this for the first customer:

{customerId: 123, shipments: [shipment1, shipment2, shipment4], amount: 50.00}

Data in json format

[
 {
   "_id": 1,
   "customerId": 123,
   "orderId": "a12b4",
   "shipments": ["shipment1","shipment2"],
   "amount": 30
 },
 {
   "_id": 2,
   "customerId": 456,
   "orderId": "a14hf",
   "shipments": ["shipment3"],
   "amount": 40
 },
 {
   "_id": 3,
   "customerId": 123,
   "orderId": "a27jd",
   "shipments": ["shipment4"],
   "amount": 20
 }
]

So, I did this:

Aggregates.unwind("$shipments"),
Aggregates.group("$customerId",
     Accumulators.sum("amount","$amount"),
     Accumulators.addToSet("shipments", "$shipments")),
Aggregates.out("test_output")

And this is where my question comes in:
Are the usages of unwind and sum exclusive?

As long as I keep my unwind statement, the sum function does not return the correct result. But it works just fine as soon as I replace shipments by another field that I do not have to unwind.

Any hints would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月棠 2025-02-15 21:47:43

查询

  • Undind将创建许多重复,但您不需要它
  • 和sum,并为每个客户收集货物
  • 可以group 阵列和与[]联合使其设置(不是重复的装运)

Playmongo

aggregate(
[{"$group": 
   {"_id": "$customerId",
    "shipments": {"$push": "$shipments"},
    "total-amound": {"$sum": "$amount"}}},
 {"$set": 
   {"shipments": 
     {"$setUnion": 
       [{"$reduce": 
           {"input": "$shipments",
            "initialValue": [],
            "in": {"$concatArrays": ["$value", "$this"]}}}, []]}}}])

Query

  • unwind will create many duplicated but you dont need it
  • you can group and sum, and collect the shipments for each customer
  • and then reduce to flatten the array, and union with [] to make it set (not duplicate shipments)

Playmongo

aggregate(
[{"$group": 
   {"_id": "$customerId",
    "shipments": {"$push": "$shipments"},
    "total-amound": {"$sum": "$amount"}}},
 {"$set": 
   {"shipments": 
     {"$setUnion": 
       [{"$reduce": 
           {"input": "$shipments",
            "initialValue": [],
            "in": {"$concatArrays": ["$value", "$this"]}}}, []]}}}])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文