- 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
A single MongoDB query to orderby date and group by user
For this, simply use aggregate() in MongoDB. Let us create a collection with documents −
> db.demo243.insertOne({"userId":1,dueDate:new ISODate("2019-01-10")}); { "acknowledged" : true, "insertedId" : ObjectId("5e4575f81627c0c63e7dba5f") } > db.demo243.insertOne({"userId":2,dueDate:new ISODate("2019-11-10")}); { "acknowledged" : true, "insertedId" : ObjectId("5e4576011627c0c63e7dba60") } > db.demo243.insertOne({"userId":2,dueDate:new ISODate("2020-01-31")}); { "acknowledged" : true, "insertedId" : ObjectId("5e4576151627c0c63e7dba61") } > db.demo243.insertOne({"userId":1,dueDate:new ISODate("2010-01-10")}); { "acknowledged" : true, "insertedId" : ObjectId("5e45761b1627c0c63e7dba62") }
Display all documents from a collection with the help of find() method −
> db.demo243.find();
This will produce the following output −
{ "_id" : ObjectId("5e4575f81627c0c63e7dba5f"), "userId" : 1, "dueDate" : ISODate("2019-01-10T00:00:00Z") } { "_id" : ObjectId("5e4576011627c0c63e7dba60"), "userId" : 2, "dueDate" : ISODate("2019-11-10T00:00:00Z") } { "_id" : ObjectId("5e4576151627c0c63e7dba61"), "userId" : 2, "dueDate" : ISODate("2020-01-31T00:00:00Z") } { "_id" : ObjectId("5e45761b1627c0c63e7dba62"), "userId" : 1, "dueDate" : ISODate("2010-01-10T00:00:00Z") }
Following is the query to orderby date and group by user −
> db.demo243.aggregate({ ... $group: { ... _id : '$userId', ... dueDate: { $max: '$dueDate'} ... } ...})
This will produce the following output −
{ "_id" : 2, "dueDate" : ISODate("2020-01-31T00:00:00Z") } { "_id" : 1, "dueDate" : ISODate("2019-01-10T00:00:00Z") }
- Related Articles
- Perform group and distinct together in a single MongoDB query
- MongoDB query to group by _id
- How to ORDER BY FIELD with GROUP BY in a single MySQL query?
- Using GROUP BY and COUNT in a single MySQL query to group duplicate records and display corresponding max value
- MongoDB query to set user defined variable into query?
- MySQL query to group results by date and display the count of duplicate values?
- MongoDB query to group duplicate documents
- MongoDB query for a single field
- MySQL- GROUP and COUNT by date?
- Sort and Group in one MongoDB aggregation query?
- MongoDB query to push a computed expression in a $group?
- How to display records having sum between a specific range using GROUP BY, HAVING and ORDER BY in a single MySQL query?
- MongoDB query to get date records in a range
- Update multiple rows in a single MongoDB query?
- Retrieve MIN and MAX date in a single MySQL query from a column with date values

Advertisements