【发布时间】:2017-10-14 10:46:08
【问题描述】:
我遇到了一些问题,需要您的帮助。
我想对下面的集合执行 mongo 聚合查询,以便我可以从 boxWeight1 和 boxWeight2 中获取每个子数组列表的总权重作为最大值的总和,即sum+=max[boxWeight1,boxWeight2] for each array item in boxList array 和其他字段应该按原样投影但具有不同键名。
收藏有点像这样……
{
_id: '',
email: 'yugal121@gmail.com',
number: 12345,
boxDetail: {
boxname: 'package_yugal',
boxList: [
{
boxWeight1: '4.0', //this is max here. so it will be added
boxWeight2: '2.0'
}.
{
boxWeight1: '4.0',
boxWeight2: '8.0' //this is max here. so it will be added
}
{
boxWeight1: '0.0',
boxWeight2: '2.0' //this is max here. so it will be added
}
]
}
}
所以上述集合在执行查询后的结果应该是这样的:
{
'User Mail': 'yugal121@gmail.com',
'Order Number': '12345',
'Total Weight': '14.0' // 4 + 8 + 2
}
我希望你能理解我的问题。 提前致谢。
【问题讨论】:
标签: arrays mongodb mongodb-query projection