我有以下mongodb文件
{
"_id" : ISODate("2016-01-04T23:00:11.000+0000"),
"value" : {
"hour" : ISODate("2016-01-04T23:00:00.000+0000"),
"day" : ISODate("2016-01-04T00:00:00.000+0000"),
"time" : ISODate("2016-01-04T23:00:11.000+0000"),
"day_chan1" : 90.162,
"day_chan2" : 77.547,
"day_chan3" : 79.32,
"total_day_chan" : 247.029
}
}
{
"_id" : ISODate("2016-01-04T23:00:23.000+0000"),
"value" : {
"hour" : ISODate("2016-01-04T23:00:00.000+0000"),
"day" : ISODate("2016-01-04T00:00:00.000+0000"),
"time" : ISODate("2016-01-04T23:00:23.000+0000"),
"day_chan1" : 90.167,
"day_chan2" : 77.549,
"day_chan3" : 79.322,
"total_day_chan" : 247.038
}
}
{
"_id" : ISODate("2016-01-04T23:00:35.000+0000"),
"value" : {
"hour" : ISODate("2016-01-04T23:00:00.000+0000"),
"day" : ISODate("2016-01-04T00:00:00.000+0000"),
"time" : ISODate("2016-01-04T23:00:35.000+0000"),
"day_chan1" : 90.172,
"day_chan2" : 77.551,
"day_chan3" : 79.324,
"total_day_chan" : 247.047
}
}
我希望根据日子文档对文档进行分组和排序,因此所有数据将在同一天出现在数组中。
到目前为止,我尝试使用
.aggregate([{
'$sort': {
'_id': 1
}
},
{
'$group' : {
'_id': '$value.day',
'data': {
'$push' : '$value'
}
}
},
{
'$project': {
'_id' : 1,
'data':1
}
}
]);
但是我注意到来自两个单独日期的数据进入同一个数组
以下是我在其中一个数组中的内容
{ hour: Tue Jan 05 2016 00:00:00 GMT+0000 (GMT),
day: Tue Jan 05 2016 00:00:00 GMT+0000 (GMT),
time: Tue Jan 05 2016 00:00:12 GMT+0000 (GMT),
day_chan1: 91.635,
day_chan2: 78.274,
day_chan3: 80.062,
total_day_chan: 249.971 }
{ hour: Mon Jan 04 2016 23:00:00 GMT+0000 (GMT),
day: Mon Jan 04 2016 00:00:00 GMT+0000 (GMT),
time: Mon Jan 04 2016 23:00:11 GMT+0000 (GMT),
day_chan1: 90.162,
day_chan2: 77.547,
day_chan3: 79.32,
total_day_chan: 247.029 }
但理想情况下,这两个应该是单独的数组
[{ hour: Tue Jan 05 2016 00:00:00 GMT+0000 (GMT),
day: Tue Jan 05 2016 00:00:00 GMT+0000 (GMT),
time: Tue Jan 05 2016 00:00:12 GMT+0000 (GMT),
day_chan1: 91.635,
day_chan2: 78.274,
day_chan3: 80.062,
total_day_chan: 249.971 }, {
},{ hour: Tue Jan 05 2016 00:0:00 GMT+0000 (GMT),
day: Tue Jan 05 2016 00:00:00 GMT+0000 (GMT),
time: Tue Jan 05 2016 01:01:12 GMT+0000 (GMT),
day_chan1: 91.635,
day_chan2: 78.274,
day_chan3: 80.062,
total_day_chan: 249.971 }],
[{ hour: Mon Jan 04 2016 23:00:00 GMT+0000 (GMT),
day: Mon Jan 04 2016 00:00:00 GMT+0000 (GMT),
time: Mon Jan 04 2016 23:00:11 GMT+0000 (GMT),
day_chan1: 90.162,
day_chan2: 77.547,
day_chan3: 79.32,
total_day_chan: 247.029 }]
同一天的所有文件都将以数组形式收集
[Date1的数组,Date2的数组,date3的数组]等等。
答案 0 :(得分:1)
您提供的示例文档具有相同的日期。所以我更改了其中一个文档的日期,然后运行了你提供的命令,得到了以下结果。
{
"_id" : ISODate("2016-01-05T00:00:00Z"),
"data" : [
{
"hour" : ISODate("2016-01-05T23:00:00Z"),
"day" : ISODate("2016-01-05T00:00:00Z"),
"time" : ISODate("2016-01-05T23:00:11Z"),
"day_chan1" : 90.162,
"day_chan2" : 77.547,
"day_chan3" : 79.32,
"total_day_chan" : 247.029
}
]
}
{
"_id" : ISODate("2016-01-04T00:00:00Z"),
"data" : [
{
"hour" : ISODate("2016-01-04T23:00:00Z"),
"day" : ISODate("2016-01-04T00:00:00Z"),
"time" : ISODate("2016-01-04T23:00:23Z"),
"day_chan1" : 90.167,
"day_chan2" : 77.549,
"day_chan3" : 79.322,
"total_day_chan" : 247.038
},
{
"hour" : ISODate("2016-01-04T23:00:00Z"),
"day" : ISODate("2016-01-04T00:00:00Z"),
"time" : ISODate("2016-01-04T23:00:35Z"),
"day_chan1" : 90.172,
"day_chan2" : 77.551,
"day_chan3" : 79.324,
"total_day_chan" : 247.047
}
]
}
所以根据我的说法,我找不到任何问题。