
- 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
How to search date between two dates in MongoDB?
To search date between two dates in MongoDB, use $gte and $lt. Let us create a collection with documents −
> db.demo524.insertOne({"EndDate":new ISODate("2020-01-19")});{ "acknowledged" : true, "insertedId" : ObjectId("5e8adbe5437efc8605595b63") } > db.demo524.insertOne({"EndDate":new ISODate("2020-01-20")});{ "acknowledged" : true, "insertedId" : ObjectId("5e8adbec437efc8605595b64") } > db.demo524.insertOne({"EndDate":new ISODate("2020-12-31")});{ "acknowledged" : true, "insertedId" : ObjectId("5e8adbf3437efc8605595b65") }
Display all documents from a collection with the help of find() method −
> db.demo524.find();
This will produce the following output −
{ "_id" : ObjectId("5e8adbe5437efc8605595b63"), "EndDate" : ISODate("2020-01-19T00:00:00Z") } { "_id" : ObjectId("5e8adbec437efc8605595b64"), "EndDate" : ISODate("2020-01-20T00:00:00Z") } { "_id" : ObjectId("5e8adbf3437efc8605595b65"), "EndDate" : ISODate("2020-12-31T00:00:00Z") }
Following is the query to search data between two dates in MongoDB −
> var first = new ISODate("2020-01-04"); > var last = new ISODate("2020-01-31"); > db.demo524.find({EndDate: {$gte: first, $lt: last}});
This will produce the following output −
{ "_id" : ObjectId("5e8adbe5437efc8605595b63"), "EndDate" : ISODate("2020-01-19T00:00:00Z") } { "_id" : ObjectId("5e8adbec437efc8605595b64"), "EndDate" : ISODate("2020-01-20T00:00:00Z") }
- Related Articles
- Perform MySQL search between two dates
- Find objects between two dates in MongoDB?
- How to check if one date is between two dates in JavaScript?
- Select the date records between two dates in MySQL
- How to list all dates between two dates in Excel?
- How to query between two dates in MySQL?
- How to create a vector with dates between two dates in R?
- How to count days between two dates in Java
- How to calculate average between two dates in Excel?
- How to calculate minutes between two dates in JavaScript?
- PHP program to find the total number of date between any two given dates
- How to get the difference between two dates in Android?
- How to calculate the difference between two dates in JavaScript?
- How to get the differences between two dates in iOS?
- How to calculate the percentage between two dates in Excel?

Advertisements