简体   繁体   中英

Mongo DB, count documents

Is there a way to query mongoDB in order to get counted documents with certain date? 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. Thank you!

Sounds like you need a simple $group stage.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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