
- 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
Aggregation: group date in nested documents (nested object) and display the count?
For aggregation, use aggregate() in MongoDB. Group the dates with $group. Let us create a collection with documents −
> db.demo717.insertOne( ... { ... "shippingdetails": ... [ ... { ... duedate:"2020-04-29 22:33:04", ... }, ... { ... duedate:"2020-03-29 22:33:04", ... }, ... { ... duedate:"2020-04-29 22:33:04", ... }, ... { ... duedate:"2020-01-29 22:33:04", ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5ea9b3cd85324c2c98cc4c30") }
Display all documents from a collection with the help of find() method −
> db.demo717.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5ea9b3cd85324c2c98cc4c30"), "shippingdetails" : [ { "duedate" : "2020-04-29 22:33:04" }, { "duedate" : "2020-03-29 22:33:04" }, { "duedate" : "2020-04-29 22:33:04" }, { "duedate" : "2020-01-29 22:33:04" } ] }
Following is the query for aggregation to group date in nested documents (nested object) −
> db.demo717.aggregate( ... { ... $unwind:"$shippingdetails"}, ... { ... $group: { ... _id: "$shippingdetails.duedate", ... count: { ... $sum: 1 ... } ... } ... } ... )
This will produce the following output −
{ "_id" : "2020-01-29 22:33:04", "count" : 1 } { "_id" : "2020-03-29 22:33:04", "count" : 1 } { "_id" : "2020-04-29 22:33:04", "count" : 2 }
- Related Articles
- Aggregation in MongoDB for nested documents?
- How to group nested fields in MongoDB aggregation with count value in array?
- Group query upon nested object in MongoDB?
- How to display only the keys from nested MongoDB documents?
- Updating Nested Embedded Documents in MongoDB?
- Find in MongoDB documents with filled nested array and reshape the documents result
- Using MongoDB nested $group and $sum to get the count of stocks with similar ProductID?
- Group objects inside the nested array JavaScript
- Update objects in a MongoDB documents array (nested updating)?
- How can I aggregate nested documents in MongoDB?\n
- Create nested JSON object in PHP?
- Print JSON nested object in JavaScript?
- Constructing a nested JSON object in JavaScript
- MySQL query to group results by date and display the count of duplicate values?
- Recursively list nested object keys JavaScript

Advertisements