我有一个每天更新文件的集合。有人可以提供一些建议,以便返回过去一个月每天添加的文件数。 我有一个创建时间戳的字段,如下所示.. “createdTimestamp”:ISODate(“2014-03-19T19:25:23.351Z”)
答案 0 :(得分:3)
您可以使用aggregation framework来实现您的目标:
db.collection.aggregate([
// Get only records created in the last 30 days
{$match:{
"createdTimestamp":{$gt: new Date(ISODate().getTime() - 1000*60*60*24*30)}
}},
// Get the year, month and day from the createdTimeStamp
{$project:{
"year":{$year:"$createdTimestamp"},
"month":{$month:"$createdTimestamp"},
"day": {$dayOfMonth:"$createdTimestamp"}
}},
// Group by year, month and day and get the count
{$group:{
_id:{year:"$year", month:"$month", day:"$day"},
"count":{$sum:1}
}}
])