如何更改MongoDB聚合结果输出?
使用此MongoDB聚合管道:如何更改MongoDB聚合结果输出?,mongodb,Mongodb,使用此MongoDB聚合管道: db.getCollection('device1_hour_events').aggregate([ { $match: { 'ts_hour' : ISODate('2013-10-11T04:00:00.000Z') } }, { $unwind: '$minutes' }, { $match: { 'minutes.min': { $gt: -1, $lt: 2 } } }, { $unwind: '$m
db.getCollection('device1_hour_events').aggregate([
{ $match: { 'ts_hour' : ISODate('2013-10-11T04:00:00.000Z') } },
{ $unwind: '$minutes' },
{ $match: { 'minutes.min': { $gt: -1, $lt: 2 } } },
{ $unwind: '$minutes.seconds' },
{ $group: { '_id': '$minutes.min',
'temp_min': { $min: '$minutes.seconds.temp' },
'temp_avg': { $avg: '$minutes.seconds.temp' },
'temp_max': { $max: '$minutes.seconds.temp' }
}
},
{ $sort: { '_id': 1} }
])
这将产生以下结果:
/* 1 */
{
"_id": 0,
"temp_min": 12,
"temp_avg": 47.25,
"temp_max": 99
}
/* 2 */
{
"_id": 1,
"temp_min": 35,
"temp_avg": 47.67,
"temp_max": 65
}
可以使用$project获得以下输出:
{
"_id": [0, 1],
"temp_min": [12, 35],
"temp_avg": [47.25, 47.67],
"temp_max": [99, 65]
}
您可以为每个字段添加另一个:
{
$group: {
'_id': 0,
'_ids': { $push: '$_id' },
'temp_min': { $push: '$temp_min' },
'temp_avg': { $push: '$temp_avg' },
'temp_max': { $push: '$temp_max' }
}
}
是的,谢谢你给我指出了正确的方向,我只是从MongoDB开始。。。我将更深入地查看文档