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

MongoDB返回过去一个月每天的文档数,第1张

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}    }}])


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5561860.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-14
下一篇 2022-12-14

发表评论

登录后才能评论

评论列表(0条)

保存