1

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。我不想更改数据,因为它来自不同的来源,我每小时都会导入一次。目前数据库中大约有 2,000,000 条记录,并且正在增加......

提出了这个问题Aggregation Framework - Converting Unix Timestamp to ISODate大约 1 个月前。如果有人有答案,也请在这里发布...

4

0 回答 0