
- 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
MongoDB query to fetch date records (ISODate format) in a range
Let us create a collection with documents −
> db.demo178.insertOne({"DueDate":new ISODate("2019-01-10T06:18:20.474Z")}); { "acknowledged" : true, "insertedId" : ObjectId("5e397bd89e4f06af551997f5") } > db.demo178.insertOne({"DueDate":new ISODate("2020-11-10T18:05:11.474Z")}); { "acknowledged" : true, "insertedId" : ObjectId("5e397bf39e4f06af551997f6") } > db.demo178.insertOne({"DueDate":new ISODate("2020-03-15T07:05:10.474Z")}); { "acknowledged" : true, "insertedId" : ObjectId("5e397c039e4f06af551997f7") } > db.demo178.insertOne({"DueDate":new ISODate("2020-06-11T16:05:10.474Z")}); { "acknowledged" : true, "insertedId" : ObjectId("5e397c0f9e4f06af551997f8") }
Display all documents from a collection with the help of find() method −
> db.demo178.find();
This will produce the following output −
{ "_id" : ObjectId("5e397bd89e4f06af551997f5"), "DueDate" : ISODate("2019-01-10T06:18:20.474Z") } { "_id" : ObjectId("5e397bf39e4f06af551997f6"), "DueDate" : ISODate("2020-11-10T18:05:11.474Z") } { "_id" : ObjectId("5e397c039e4f06af551997f7"), "DueDate" : ISODate("2020-03-15T07:05:10.474Z") } { "_id" : ObjectId("5e397c0f9e4f06af551997f8"), "DueDate" : ISODate("2020-06-11T16:05:10.474Z") }
Following is the query to fetch date records in a range −
> db.demo178.aggregate([ ...{ ... "$redact": { ... "$cond": { ... "if": { ... "$and": [ ... { "$gt": [ {"$hour": "$DueDate"}, 5] }, ... { "$lt": [ {"$hour": "$DueDate"}, 9] } ... ] ... }, ... "then": "$$KEEP", ... "else": "$$PRUNE" ... } ... } ... } ...])
This will produce the following output −
{ "_id" : ObjectId("5e397bd89e4f06af551997f5"), "DueDate" : ISODate("2019-01-10T06:18:20.474Z") } { "_id" : ObjectId("5e397c039e4f06af551997f7"), "DueDate" : ISODate("2020-03-15T07:05:10.474Z") }
- Related Articles
- MongoDB query to get date records in a range
- UNIX_TIMESTAMP with date in MySQL query to fetch records after a specific date in different format?
- How to work Date query with ISODate in MongoDB?
- MySQL query to fetch records from a range of months?
- MySQL query to fetch the latest date from a table with date records
- Get component of Date / ISODate in MongoDB?
- Fetch month, day, year, etc. from ISODate in MongoDB?
- MongoDB query to fetch elements between a range excluding both the numbers used to set range?
- How to filter a query on specific date format with MongoDB?
- MySQL query to fetch date records greater than the current date after adding days with INTERVAL?
- MySQL time period query to fetch date records from interval of 14 weeks from current date?
- MongoDB query to search date records using only Month and Day
- Using aggregation pipeline to fetch records in MongoDB
- MySQL query to find a match and fetch records
- MongoDB query to fetch array values

Advertisements