- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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") }
Advertisements