我遇到运行以下聚合查询的问题:
db.snippets.aggregate([ { '$project': { month: { '$month': '$created_at' }} } ])
Run Code Online (Sandbox Code Playgroud)
相同的错误消息是:
assert: command failed: {
"errmsg" : "exception: can't convert from BSON type EOO to Date",
"code" : 16006,
"ok" : 0 } : aggregate failed
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个问题.我找到了一个相关的问题
但它并没有说明如何完成任务.
我试图按小时聚合MongoDB colloection中的记录,并需要将存储为timestamp(毫秒)的日期转换为ISODate,以便我可以使用聚合框架的内置日期运算符($ hour,$ month等)
记录存储为
{
"data" : { "UserId" : "abc", "ProjId" : "xyz"},
"time" : NumberLong("1395140780706"),
"_id" : ObjectId("532828ac338ed9c33aa8eca7")
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用以下类型的聚合查询:
db.events.aggregate(
{
$match : {
"time" : { $gte : 1395186209804, $lte : 1395192902825 }
}
},
{
$project : {
_id : "$_id",
dt : {$concat : (Date("$time")).toString()} // need to project as ISODate
}
},
// process records further in $project or $group clause
)
Run Code Online (Sandbox Code Playgroud)
产生以下形式的结果:
{
"result" : [
{
"_id" : ObjectId("5328da21fd207d9c3567d3ec"),
"dt" …Run Code Online (Sandbox Code Playgroud)