我有以下格式的大约20,000个文档,其中value.day给了我日期
{
"_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
}
}
我想执行一个聚合,它会从数组中的每一天给我数据。运行以下命令给了我两天但没有数据。
db.temp4437190046D9electricity.aggregate([
{
$group: {
'_id': '$value.day'
}
}
]);
{ "_id" : ISODate("2016-01-05T00:00:00Z") }
{ "_id" : ISODate("2016-01-04T00:00:00Z") }
如何将这两个ID的相应数据输入这些对象?
预期输出看起来像
{ "_id" : ISODate("2016-01-04T00:00:00Z"),
data: [{
"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
}
{
"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
}
]
},
{ "_id" : ISODate("2016-01-04T00:00:00Z"),
"data":[
{
"hour" : ISODate("2016-01-05T14:00:00.000+0000"),
"day" : ISODate("2016-01-05T00:00:00.000+0000"),
"time" : ISODate("2016-01-05T14:16:44.000+0000"),
"day_chan1" : 63.611,
"day_chan2" : 56.801,
"day_chan3" : 58.129,
"total_day_chan" : 178.541
}
{
"hour" : ISODate("2016-01-05T14:00:00.000+0000"),
"day" : ISODate("2016-01-05T00:00:00.000+0000"),
"time" : ISODate("2016-01-05T14:16:56.000+0000"),
"day_chan1" : 63.644,
"day_chan2" : 56.833,
"day_chan3" : 58.161,
"total_day_chan" : 178.638
}
]
}
答案 0 :(得分:1)
此命令将为您提供预期的输出。您的聚合命令中缺少$push运算符。
db.test.aggregate([
{
'$group' : {
'_id': '$value.day',
'data': {
'$push' : '$value'
}
}
}
]).pretty()
答案 1 :(得分:0)
您需要使用$push
阶段中的$group
累加器运算符为一组共享同一组的文档中的每个文档返回值数组键;这里value.day
。此外,您不应使用$$ROOT
变量,因为对于大型收集,生成的文档可能会超过BSON Document Size limit。
db.temp4437190046D9electricity.aggregate([
{ "$group": {
"_id": "$value.day",
"data": { "$push":
{
"hour" : "$value.hour",
"day" : "$value.day",
"time" : "$value.time",
"day_chan1" : "$value.day_chan1",
"day_chan2" : "$value.day_chan2",
"day_chan3" : "$value.day_chan3",
"total_day_chan" : "$value.total_day_chan"
}
}
}}
])
使用您提供的样本数据:
{
"_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:11Z"),
"day_chan1" : 90.162,
"day_chan2" : 77.547,
"day_chan3" : 79.32,
"total_day_chan" : 247.029
},
{
"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
}
]
}