我已经按照以下方式计算了我的日期
示例数据:
{
"_id" : ObjectId("5f703ef9db38661df02d3a77"),
"color" : "black",
"icon" : "grav",
"name" : "viewed count",
"created_date" : ISODate("2020-09-27T07:27:53.816Z"),
"updated_date" : ISODate("2021-05-11T19:11:14.415Z"),
"read" : false,
"graph" : [
{
"_id" : ObjectId("5f704286db38661df02d3a81"),
"count" : 1,
"date" : ISODate("2020-09-27T07:43:02.231Z")
},
{
"_id" : ObjectId("5f7043ebdb38661df02d3a88"),
"date" : ISODate("2020-09-27T07:48:59.383Z"),
"count" : 2
}]
}
我的问题是:
db.test.aggregate(
[{$match:{"name" : "viewed count"}},
{$unwind:'$graph'},
{
$match: {
'graph.date': {
$gt: ISODate("2018-09-27"),
$lt: ISODate("2021-09-27")
}
}
},
{
$project: {
date: {$dateToString: {format: '%Y-%m-%d', date: '$graph.date'}}
},
},
{
$group: {
_id: "$date",
count: {$sum: 1}
}
},
{
$sort: {
_id: 1
}
}
]
)
输出:
[{
"_id" : "2020-09-27",
"count" : 58.0
},
{
"_id" : "2020-09-29",
"count" : 50.0
}]