Java Mongo DB聚合管道 - $ sum在使用$ Undind时无法正常工作吗?
我目前正在我的一个Java脚本中使用MongoDB聚合管道。
输入数据可以简化为不同客户的订单列表。例如:
_id | customerId | OrderId | 运输 | 金额 |
---|---|---|---|---|
1 | 123 | A12B4 | 货运1,装运 | 2 30.00 |
2 | 456 | A14HF | 装运3 | 40.00 |
2 | 123 | A27JD | 货运4 | 20.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:
_id | customerId | orderId | shipments | amount |
---|---|---|---|---|
1 | 123 | a12b4 | shipment1, shipment2 | 30.00 |
2 | 456 | a14hf | shipment3 | 40.00 |
2 | 123 | a27jd | shipment4 | 20.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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查询
Undind
将创建许多重复,但您不需要它group
阵列和与[]
联合使其设置(不是重复的装运)Playmongo
Query
unwind
will create many duplicated but you dont need itgroup
and sum, and collect the shipments for each customerreduce
to flatten the array, andunion with []
to make it set (not duplicate shipments)Playmongo