因為mongodb是按照utc時間存儲 比中國的時區(qū)要少8個小時 如果直接變成字符串聚合是不正確的
db.getCollection("xxxxx").aggregate([
{
$match: {
created_at: {$gte: ISODate('2021-01-01'),$lte: ISODate("2021-03-12")}
}
},{
$project: {
created_at: 1,
created_day: {
$dateToString: {
format: "%Y-%m-%d",
date: {
"$add": [ "$created_day", 28800000]
}
}
}
}
},{
$group: {
_id: "$created_day",
count: {$sum: 1}
}
},{
}
])