![](/img/trans.png)
[英]How do I do a multi tier count breakdown using Aggregation in mongo DB?
[英]using mongo aggregation how to get records with count
我在24小时记录中尝试了此代码,但我的要求是所有24小时记录和记录总数
db.getCollection("COLLECTION_NAME").find({"createdAt":{$gt:new Date(Date.now() - 24*60*60 * 1000)}})
如果您可以提供2,3个集合来测试此查询,答案将更为准确。 无论如何,这就是您使用aggregation
框架的方式。
db.collection.aggregate(
[
{$match : {"createdAt":{$gt:new Date(Date.now() - 24*60*60 * 1000)}}},
{ "$group": {
"_id": null,
"data": { "$push": "$$ROOT" },
"count": { "$sum": 1 }
}},
{ "$unwind": "$data" },
{ "$project ": {"_id" : 0}}
]
)
集合中的记录总数为
db.getCollection(“ myCollection”)。count()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.