- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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 a MongoDB collection?
To group by dates, use $group in MongoDB aggregate. Let us create a collection with documents −
> db.demo657.insertOne({ ... id: 1, ... Name: "Chris", ... DueDate: new ISODate("2020-04-22") ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5ea064b44deddd72997713d6") } > db.demo657.insertOne( ... { ... id: 1, ... Name: "John", ... DueDate: new ISODate("2020-04-22") ... } ...); { "acknowledged" : true, "insertedId" : ObjectId("5ea064b44deddd72997713d7") } > db.demo657.insertOne( ... { ... id: 1, ... Name: "Chris", ... DueDate: new ISODate("2020-04-22") ... } ...); { "acknowledged" : true, "insertedId" : ObjectId("5ea064b44deddd72997713d8") }
Display all documents from a collection with the help of find() method −
> db.demo657.find();
This will produce the following output −
{ "_id" : ObjectId("5ea064b44deddd72997713d6"), "id" : 1, "Name" : "Chris", "DueDate" : ISODate("2020-04-22T00:00:00Z") } { "_id" : ObjectId("5ea064b44deddd72997713d7"), "id" : 1, "Name" : "John", "DueDate" : ISODate("2020-04-22T00:00:00Z") } { "_id" : ObjectId("5ea064b44deddd72997713d8"), "id" : 1, "Name" : "Chris", "DueDate" : ISODate("2020-04-22T00:00:00Z") }
Following is the query to group by dates in MongoDB collection −
> db.demo657.aggregate([ ... {$match: {id:1, DueDate: {$gte:new ISODate('2020-04-21'), $lt:new ISODate ("2020-04-23")}}}, ... {$project: ... {day: {'$dayOfMonth': '$DueDate'},month: {'$month':'$DueDate'},year: {'$year':'$DueDate'},Name:"$Name" } ... }, ... {$group: { ... _id: {day:'$day',month:'$month',year:'$year'}, ... ListOfNames: {$push:{Name:'$Name'}} ... }} ... ])
This will produce the following output −
{ "_id" : { "day" : 22, "month" : 4, "year" : 2020 }, "ListOfNames" : [ { "Name" : "Chris" }, { "Name" : "John" }, { "Name" : "Chris" } ] }
- Related Articles
- Group by dates in MongoDB?
- How can I aggregate collection and group by field count in MongoDB?
- Get MongoDB documents with max attribute per group in a collection?
- Matching MongoDB collection items by id?
- Sort MongoDB Collection by Array value?
- MongoDB SELECT COUNT GROUP BY?
- Clone a collection in MongoDB?
- MongoDB query to group by _id
- Find all duplicate documents in a MongoDB collection by a key field?
- Retrieving specific documents from collection by _id in MongoDB
- Find values group by another field in MongoDB?
- Retrieving group by result with arrays in MongoDB?
- Getting a list of values by using MongoDB $group?
- Create index in a MongoDB collection?
- Fetch data between two dates and with a specific value in MongoDB. Group and get the sum with count?

Advertisements