
- MongoDB Tutorial
- MongoDB - Home
- MongoDB - Overview
- MongoDB - Advantages
- MongoDB - Environment
- MongoDB - Data Modeling
- MongoDB - Create Database
- MongoDB - Drop Database
- MongoDB - Create Collection
- MongoDB - Drop Collection
- MongoDB - Data Types
- MongoDB - Insert Document
- MongoDB - Query Document
- MongoDB - Update Document
- MongoDB - Delete Document
- MongoDB - Projection
- MongoDB - Limiting Records
- MongoDB - Sorting Records
- MongoDB - Indexing
- MongoDB - Aggregation
- MongoDB - Replication
- MongoDB - Sharding
- MongoDB - Create Backup
- MongoDB - Deployment
- MongoDB - Java
- MongoDB - PHP
- Advanced MongoDB
- MongoDB - Relationships
- MongoDB - Database References
- MongoDB - Covered Queries
- MongoDB - Analyzing Queries
- MongoDB - Atomic Operations
- MongoDB - Advanced Indexing
- MongoDB - Indexing Limitations
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - Text Search
- MongoDB - Regular Expression
- Working with Rockmongo
- MongoDB - GridFS
- MongoDB - Capped Collections
- Auto-Increment Sequence
- MongoDB Useful Resources
- MongoDB - Questions and Answers
- MongoDB - Quick Guide
- MongoDB - Useful Resources
- MongoDB - Discussion
Group by dates in MongoDB?
You can use aggregate framework to group by dates in MongoDB. Let us first create a collection with some documents. The query to create a collection with documents are as follows:
> db.groupByDateDemo.insertOne({"UserLoginDateTime":new ISODate()}); { "acknowledged" : true, "insertedId" : ObjectId("5c6ee4df6fd07954a4890695") } > db.groupByDateDemo.insertOne({"UserLoginDateTime":new ISODate("2019-01-31 15:20:09.234Z")}); { "acknowledged" : true, "insertedId" : ObjectId("5c6ee51c6fd07954a4890696") } > db.groupByDateDemo.insertOne({"UserLoginDateTime":new ISODate("2017-04-21 16:12:13.240Z")}); { "acknowledged" : true, "insertedId" : ObjectId("5c6ee5336fd07954a4890697") } > db.groupByDateDemo.insertOne({"UserLoginDateTime":new ISODate("2016-05-25 19:11:21.130Z")}); { "acknowledged" : true, "insertedId" : ObjectId("5c6ee54b6fd07954a4890698") } > db.groupByDateDemo.insertOne({"UserLoginDateTime":new ISODate("2016-05-25 19:11:21.130Z")}); { "acknowledged" : true, "insertedId" : ObjectId("5c6ee8de6fd07954a4890699") } > db.groupByDateDemo.insertOne({"UserLoginDateTime":new ISODate()}); { "acknowledged" : true, "insertedId" : ObjectId("5c6ee8e76fd07954a489069a") }
Display all documents from a collection with the help of find() method. The query is as follows:
> db.groupByDateDemo.find().pretty();
The following is the output:
{ "_id" : ObjectId("5c6ee4df6fd07954a4890695"), "UserLoginDateTime" : ISODate("2019-02-21T17:50:23.076Z") } { "_id" : ObjectId("5c6ee51c6fd07954a4890696"), "UserLoginDateTime" : ISODate("2019-01-31T15:20:09.234Z") } { "_id" : ObjectId("5c6ee5336fd07954a4890697"), "UserLoginDateTime" : ISODate("2017-04-21T16:12:13.240Z") } { "_id" : ObjectId("5c6ee54b6fd07954a4890698"), "UserLoginDateTime" : ISODate("2016-05-25T19:11:21.130Z") } { "_id" : ObjectId("5c6ee8de6fd07954a4890699"), "UserLoginDateTime" : ISODate("2016-05-25T19:11:21.130Z") } { "_id" : ObjectId("5c6ee8e76fd07954a489069a"), "UserLoginDateTime" : ISODate("2019-02-21T18:07:35.208Z") }
Here is the query to group by dates in MongoDB:
> db.groupByDateDemo.aggregate([ ... { $group: { ... _id: { ... $add: [ ... { $dayOfYear: "$UserLoginDateTime"}, ... { $multiply: ... [400, {$year: "$UserLoginDateTime"}] ... } ... ]}, ... NumberOfTimes: { $sum: 1 }, ... f: {$min: "$UserLoginDateTime"} ... } ... }, ... { $sort: {_id: 1} }, ... { $limit: 10 }, ... { $project: { date: "$f", NumberOfTimes: 1, _id: 0} } ... ]);
The following is the output:
{ "NumberOfTimes" : 2, "date" : ISODate("2016-05-25T19:11:21.130Z") } { "NumberOfTimes" : 1, "date" : ISODate("2017-04-21T16:12:13.240Z") } { "NumberOfTimes" : 1, "date" : ISODate("2019-01-31T15:20:09.234Z") } { "NumberOfTimes" : 2, "date" : ISODate("2019-02-21T17:50:23.076Z") }
- Related Articles
- Group by dates in a MongoDB collection?
- MongoDB SELECT COUNT GROUP BY?
- MongoDB query to group by _id
- Find values group by another field in MongoDB?
- Retrieving group by result with arrays in MongoDB?
- Find objects between two dates in MongoDB?
- Getting a list of values by using MongoDB $group?
- Fetch data between two dates and with a specific value in MongoDB. Group and get the sum with count?
- Group by day/month/week based on the date range in MongoDB
- How can I aggregate collection and group by field count in MongoDB?
- How to search date between two dates in MongoDB?
- Group across two columns in MongoDB?
- Multilevel $group using MongoDB
- A single MongoDB query to orderby date and group by user
- Group query upon nested object in MongoDB?

Advertisements