您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

MongoDB返回过去一个月每天的文档数

MongoDB返回过去一个月每天的文档数

您可以使用聚合框架来实现所需的功能

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}
    }}
])
mongodb 2022/1/1 18:27:02 有364人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶