- 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 day/month/week based on the date range in MongoDB
To group, use $week and $month in MongoDB. Let us create a collection with documents −
> db.demo133.insertOne({"Rank":18,"DueDate":new ISODate("2020-01-10")}); { "acknowledged" : true, "insertedId" : ObjectId("5e31980968e7f832db1a7f78") } > db.demo133.insertOne({"Rank":12,"DueDate":new ISODate("2020-01-10")}); { "acknowledged" : true, "insertedId" : ObjectId("5e31982568e7f832db1a7f79") } > db.demo133.insertOne({"Rank":12,"DueDate":new ISODate("2020-02-01")}); { "acknowledged" : true, "insertedId" : ObjectId("5e31986568e7f832db1a7f7a") } > db.demo133.insertOne({"Rank":20,"DueDate":new ISODate("2020-02-01")}); { "acknowledged" : true, "insertedId" : ObjectId("5e31986c68e7f832db1a7f7b") }
Display all documents from a collection with the help of find() method −
> db.demo133.find();
This will produce the following output −
{ "_id" : ObjectId("5e31980968e7f832db1a7f78"), "Rank" : 18, "DueDate" : ISODate("2020-01-10T00:00:00Z") } { "_id" : ObjectId("5e31982568e7f832db1a7f79"), "Rank" : 12, "DueDate" : ISODate("2020-01-10T00:00:00Z") } { "_id" : ObjectId("5e31986568e7f832db1a7f7a"), "Rank" : 12, "DueDate" : ISODate("2020-02-01T00:00:00Z") } { "_id" : ObjectId("5e31986c68e7f832db1a7f7b"), "Rank" : 20, "DueDate" : ISODate("2020-02-01T00:00:00Z") }
Following is the query to group by day/month/week based on the date range −
> db.demo133.aggregate([ ... { ... "$project": { ... "DueDateWeek": { "$week": "$DueDate" }, ... "DueDateMonth": { "$month": "$DueDate" }, ... "Rank": 1 ... } ... }, ... { ... "$group": { ... "_id": "$DueDateWeek", ... "AvgValue": { "$avg": "$Rank" }, ... "MonthValue": { "$first": "$DueDateMonth" } ... } ... } ... ])
This will produce the following output −
{ "_id" : 4, "AvgValue" : 16, "MonthValue" : 2 } { "_id" : 1, "AvgValue" : 15, "MonthValue" : 1 }
- Related Articles
- Finding day of week from date (day, month, year) in JavaScript
- How to get day of month, day of year and day of week in android using offset date time API class?
- Return query based on date in MongoDB?
- MongoDB query to search date records using only Month and Day
- In MySQL, how we can compute date by providing the year, week number and day of the week?\nday of the week?
- Determine day of week in month from Gregorian Calendar in Java
- Querying array of Embedded Documents in MongoDB based on Range?
- Java Program to get day of week for the last day of each month in 2019
- Get the day of week for a particular date in Java
- Create date from day, month, year fields in MySQL?
- Crontab day of the week syntax on Linux
- Get the Day of the Week from Today's Date in Java
- Fetch month, day, year, etc. from ISODate in MongoDB?
- Compare only day and month with date field in MySQL?
- How to convert year, month, and day of the month into a complete date in R?

Advertisements