java - How to aggregate over map keys across all the documents in mongodB collection?
问题描述
I have data in the following format:
[{
"OrderId": "406-5309498-5972326",
"revenueHeader": {
"Principal": 982.14,
"Product Tax": 117.86,
"Total": 1100
}},
{
"OrderId": "506-5234568-5934567",
"revenueHeader": {
"Principal": 382.54,
"Product Tax": 34.46,
"Shipping charge": 30.5,
"Giftwrap charge": 27.5,
"Total": 234
}}]
How can I sum the revenueHeader map values for the keys across all the documents? Note: "Shipping charge" is not present in the first document but we still want the sum for this key across all the documents. Also, the keys can vary, there is no way to know the name of keys beforehand.
解决方案
You have to use $objectToArray to transform revenueHeader in an array of {k,v} object.
You can then $unwind the array, and group by revenueHeader.k, summing revenueHeader.v . By this way, you never take care of the fields name or presence inside revenueHeader.
Here's the query :
db.collection.aggregate([
{
$project: {
revenueHeader: {
$objectToArray: "$revenueHeader"
}
}
},
{
$unwind: "$revenueHeader"
},
{
$group: {
_id: "$revenueHeader.k",
total: {
$sum: "$revenueHeader.v"
}
}
}
])
You can test it here
推荐阅读
- unity3d - Unity 麦克风开始录制时有延迟
- python - 如何使用 python 格式函数替换值?
- python - OSError:[WinError 10057] 无法通过套接字发送任何内容
- android - 无法在 ActionBar 中放置元素
- mysql - MySQL 总时间正在增加
- powershell - PowerShell splatting SecureString is converted to String
- tortoisegit - Push a local branch to origin using TortoiseGit
- google-translate - 使用付费谷歌翻译 api 的网页翻译器?
- python - 矩阵尺寸与 MNIST 时尚不兼容
- postgresql - PostgreSQL crosstab query with multiple columns and week day