MongoDB中的聚合
收藏品块
{
"_id": ObjectId("512eef329d5d0c9415000025"),
"tx": {
"0": "A1",
"1": "A2",
"2": "A3"
},
"time": NumberInt(1304200205)
}
{
"_id": ObjectId("512eef329d5d0c9415000026"),
"tx": {
"0": "A4",
"1": "A5",
"2": "A6"
},
"time": NumberInt(1304200395)
}
{
"_id": ObjectId("512eef329d5d0c9415000027"),
"tx": {
"0": "A7",
"1": "A8",
"2": "A9"
},
"time": NumberInt(1304202305)
}
db.blocks.aggregate(
{'$unwind':'$tx'},
{'$group' : { '_id' : {
'year': {'$year':'$time'},
'month': {'$year':'$time'},
},
'count' : {'$sum':1}
}
});
{
"errmsg" : "exception: can't convert from BSON type NumberInt32 to Date",
"code" : 16006,
"ok" : 0
}
这意味着时间字段不是Mongo Date格式,而是NumberInt32。
如何使用聚合函数将其转换为MongoDate。我不想更改数据,因为它来自不同的来源,我每小时导入一次。目前DB中有大约2,000,000条记录正在增加......
问题在约1个月前提出Aggregation Framework - Converting Unix Timestamp to ISODate。如果有人有答案,请在这里发帖...