【发布时间】:2017-04-22 13:12:19
【问题描述】:
我正在尝试根据年、月、日对我的 mongodb 查询进行分组。
我的数据存储在数据库如下:
{
"installAt" : "2016-08-01T10:24:38.502Z",
"success" : true
"app" : "web"
}
我试图在上面实现的查询:
db.sampleCollection.aggregate(
[
{
$group : {
_id : { month: { $month: "$installAt" }, day: { $dayOfMonth: "$installAt" }, year: { $year: "$installAt" } },
count: { $sum: 1 }
}
}
]
)
但我收到此错误:
assert: command failed: {
"errmsg" : "exception: can't convert from BSON type EOO to Date",
"code" : 16006,
"ok" : 0
} : aggregate failed
Error: command failed: {
"errmsg" : "exception: can't convert from BSON type EOO to Date",
"code" : 16006,
"ok" : 0
} : aggregate failed
at Error (<anonymous>)
at doassert (src/mongo/shell/assert.js:11:14)
at Function.assert.commandWorked (src/mongo/shell/assert.js:244:5)
at DBCollection.aggregate (src/mongo/shell/collection.js:1149:12)
at (shell):1:23
2016-12-07T19:48:25.541+0530 Error: command failed: {
"errmsg" : "exception: can't convert from BSON type EOO to Date",
"code" : 16006,
"ok" : 0
} : aggregate failed at src/mongo/shell/assert.js:13
这个例外的一个原因可能是(我不确定)因为
"installAt" : "2016-08-01T10:24:38.502Z"
不是mongodb日期格式而是String。
如果这是问题所在,那么解决此问题的一种方法是将这些字段更新为 ISODate 格式,例如
ISODate("2016-08-01T10:24:38.502Z")
但由于某些原因,我目前无法修改我的数据库。
我可以使用上述代码或替代代码实现分组吗? (不更新数据库)
【问题讨论】:
标签: mongodb mongodb-query