我在Mongo中有以下数据,并尝试按天对搜索进行分组。所以我知道每天发生多少搜索
{
"_id" : ObjectId("5ee3ebb8a18fae0001cdf0c6"),
"term" : "wp-login.php",
"Date" : ISODate("2020-06-12T20:55:20.335Z")
}
我有以下查询
db.search.aggregate(
{
$project: {
year: {$year: Date},
month: {$month: Date},
dayOfMonth: {$dayOfMonth: Date}
}
},
{
$group: {
_id: {
year: '$year',
month: '$month',
dayOfMonth: '$dayOfMonth'
},
count: {
$sum: 1
}
}
}
)
我得到以下错误
执行脚本失败。
错误:命令失败:{"ok": 0,"errmsg":"优化管道:: 失败导致:: 无法从BSON类型javascript转换为Date","code":16006,"codeName":"Location16006"}:聚合失败详细信息:_getErrorWithCode@src/mongo/shell/utils.js:25:13doassert@src/mongo/shell/assert.js:18:14_assertCommandWorked@src/mongo/shell/assert.js:534:17assert.commandWorked@src/mongo/shell/assert.js:618:16DB.prototype._runAggregate@src/mongo/shell/db.js:260:9DBCollection.prototype.aggregate@src/mongo/shell/集合.js:1062:12 DBCollection.原型@:1:355@(shell):1:1
使用“$Date”而不是Date
db.collection.aggregate({
$project: {
year: {
$year: "$Date"
},
month: {
$month: "$Date"
},
dayOfMonth: {
$dayOfMonth: "$Date"
}
}
},
{
$group: {
_id: {
year: "$year",
month: "$month",
dayOfMonth: "$dayOfMonth"
},
count: {
$sum: 1
}
}
})
你可以参考这里的例子