
- 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
Find objects between two dates in MongoDB?
Use operator $gte and $lt to find objects between two dates in MongoDB. To understand these operators, let us create a collection.
Creating a collection here:
>db.order.insert({"OrderId":1,"OrderAddrees":"US","OrderDateTime":ISODate("2019-02-19")}; WriteResult({ "nInserted" : 1 }) >db.order.insert({"OrderId":2,"OrderAddrees":"UK","OrderDateTime":ISODate("2019-02-26")}; WriteResult({ "nInserted" : 1 })
Display all documents from the collection with the help of find() method. The query is as follows:
> db.order.find().pretty();
The following is the output:
{ "_id" : ObjectId("5c6c072068174aae23f5ef57"), "OrderId" : 1, "OrderAddrees" : "US", "OrderDateTime" : ISODate("2019-02-19T00:00:00Z") } { "_id" : ObjectId("5c6c073568174aae23f5ef58"), "OrderId" : 2, "OrderAddrees" : "UK", "OrderDateTime" : ISODate("2019-02-26T00:00:00Z") }
Here is the query to find objects between two dates:
> db.order.find({"OrderDateTime":{ $gte:ISODate("2019-02-10"), $lt:ISODate("2019-02-21") } }).pretty();
The following is the output:
{ "_id" : ObjectId("5c6c072068174aae23f5ef57"), "OrderId" : 1, "OrderAddrees" : "US", "OrderDateTime" : ISODate("2019-02-19T00:00:00Z") }
- Related Articles
- How to search date between two dates in MongoDB?
- Find the number of logins between two dates in MySQL
- Find number of days between two given dates in C++
- How to list all dates between two dates in Excel?
- Return all dates between two dates in an array in PHP
- Find the count of users who logged in between specific dates with MongoDB
- Calculate minutes between two dates in C#
- How to create a vector with dates between two dates in R?
- Perform MySQL search between two dates
- Python program to find number of days between two given dates
- How to query between two dates in MySQL?
- Python Pandas – Filter DataFrame between two dates
- Group by dates in MongoDB?
- How to count days between two dates in Java
- Select the date records between two dates in MySQL

Advertisements