
- 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
Fetch data between two dates and with a specific value in MongoDB. Group and get the sum with count?
To match, use $match in MongoDB and to get data between two dates, use $gte and $lte. Let us create a collection with documents −
> db.demo560.insertOne({"value1":40,"value2":40,shippingDate:new ISODate("2020-02-26")});{ "acknowledged" : true, "insertedId" : ObjectId("5e8f3d5254b4472ed3e8e867") } > db.demo560.insertOne({"value1":20,"value2":60,shippingDate:new ISODate("2020-02-26")});{ "acknowledged" : true, "insertedId" : ObjectId("5e8f3d5254b4472ed3e8e868") } > db.demo560.insertOne({"value1":40,"value2":70,shippingDate:new ISODate("2020-03-31")});{ "acknowledged" : true, "insertedId" : ObjectId("5e8f3d5254b4472ed3e8e869") } > db.demo560.insertOne({"value1":40,"value2":130,shippingDate:new ISODate("2020-03-31")});{ "acknowledged" : true, "insertedId" : ObjectId("5e8f3d5254b4472ed3e8e86a") }
Display all documents from a collection with the help of find() method −
> db.demo560.find();
This will produce the following output −
{ "_id" : ObjectId("5e8f3d5254b4472ed3e8e867"), "value1" : 40, "value2" : 40, "shippingDate" : ISODate("2020-02-26T00:00:00Z") } { "_id" : ObjectId("5e8f3d5254b4472ed3e8e868"), "value1" : 20, "value2" : 60, "shippingDate" : ISODate("2020-02-26T00:00:00Z") } { "_id" : ObjectId("5e8f3d5254b4472ed3e8e869"), "value1" : 40, "value2" : 70, "shippingDate" : ISODate("2020-03-31T00:00:00Z") } { "_id" : ObjectId("5e8f3d5254b4472ed3e8e86a"), "value1" : 40, "value2" : 130, "shippingDate" : ISODate("2020-03-31T00:00:00Z") }
Following is the query to fetch data between two dates and with a specific value. Here, value1 40 is our specific value −
> db.demo560.aggregate([ ... { ... $match: { ... "value1": 40, ... "shippingDate": { ... "$gte": ISODate("2020-02-26"), ... "$lte": ISODate("2020-03-31") ... } ... } ... }, ... { ... $group: { ... "_id": "$shippingDate", ... total: { ... $sum: '$value2' ... }, ... count: { ... $sum: 1 ... } ... } ... } ... ])
This will produce the following output −
{ "_id" : ISODate("2020-03-31T00:00:00Z"), "total" : 200, "count" : 2 } { "_id" : ISODate("2020-02-26T00:00:00Z"), "total" : 40, "count" : 1 }
- Related Articles
- Find the count of users who logged in between specific dates with MongoDB
- Using MongoDB nested $group and $sum to get the count of stocks with similar ProductID?
- How to count and sum a field between 2 dates in MongoDB?
- Get the count of a specific value in MongoDB
- MongoDB aggregation to fetch documents with specific field value?
- Match MongoDB documents with field value greater than a specific number and fetch them?
- How to query MongoDB a value with $lte, $in and $not to fetch specific values?
- MongoDB query to group records and display a specific value with dot notation
- Get the count of a specific value in MongoDB quickly
- Count numbers with difference between number and its digit sum greater than specific value in C++
- JavaScript fetch a specific value with eval()?
- Fetch a specific document in MongoDB with array elements
- How to get specific data in different formats with MongoDB?
- MySQL query to count the dates and fetch repeated dates as well
- Get the difference between dates and calculate salary with MySQL?

Advertisements