简体   繁体   English

Mongo DB,计数文档

[英]Mongo DB, count documents

Is there a way to query mongoDB in order to get counted documents with certain date?有没有办法查询 mongoDB 以获得特定日期的计数文档? As for example, I have documents:例如,我有文件:

[{
fullDate: '2020/02/01',
someData: 'someData',
},
{
fullDate: '2020/03/01',
someData: 'someData',
},
{
fullDate: '2020/03/01',
someData: 'someData',
},
{
fullDate: '2020/02/01',
someData: 'someData',
},
{
fullDate: '2020/02/01',
someData: 'someData',
},
{
fullDate: '2020/02/01',
someData: 'someData',
}]

I would like to make counting query to get info that there are 4 documents with fullDate of 2020/02/01 and 2 documents with fullDate of 2020/02/03 in one query.我想进行计数查询以获取在一个查询中有 4 个文档的 fullDate 为 2020/02/01 和 2 个文档的 fullDate 为 2020/02/03 的信息。 Thank you!谢谢!

Sounds like you need a simple $group stage.听起来您需要一个简单的$group阶段。

db.collection.aggregate([
    {
        $group: {
            _id: "$fullDate",
            count: {$sum: 1}
        }
    }
])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM