这是查询
[
{
"$project": {
"formattedDate": {
"$dateToString": { "format": "%Y-%m-%d", "date": "$ceatedAt" }
},
"createdAtMonth": { "$month": "$ceatedAt" },
"rating": 1
}
},
{
"$group": {
"_id": "$formattedDate",
"average": { "$avg": "$rating" },
"month": { "$first": "$createdAtMonth" },
}
}
]
我需要时间戳中的日期。如何做到这一点?
最佳答案
Mongodb 4.0 已推出 $toLong
将日期转换为时间戳的聚合
db.collection.aggregate([
{ "$project": {
"createdAt": {
"$toLong": "$createdAt"
}
}}
])
你可以试试here
关于mongodb - 在mongo查询中将iso日期转换为时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51802016/