
- 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
Query MongoDB for a datetime value less than NOW?
You can use $lte operator along with new Date() for this. Let us first create a collection with documents
>db.dateTimeValueLessThanNowDemo.insertOne({"CustomerName":"Larry","CustomerProductName":"Product-1","ArrivalDate":new ISODate("2017-01-31")}); { "acknowledged" : true, "insertedId" : ObjectId("5ca1e8ab66324ffac2a7dc59") } >db.dateTimeValueLessThanNowDemo.insertOne({"CustomerName":"Mike","CustomerProductName":"Product-2","ArrivalDate":new ISODate("2019-04-01")}); { "acknowledged" : true, "insertedId" : ObjectId("5ca1e8c166324ffac2a7dc5a") } >db.dateTimeValueLessThanNowDemo.insertOne({"CustomerName":"Chris","CustomerProductName":"Product-3","ArrivalDate":new ISODate("2019-03-31")}); { "acknowledged" : true, "insertedId" : ObjectId("5ca1e8d266324ffac2a7dc5b") } >db.dateTimeValueLessThanNowDemo.insertOne({"CustomerName":"Robert","CustomerProductName":"Product-4","ArrivalDate":new ISODate("2019-04-02")}); { "acknowledged" : true, "insertedId" : ObjectId("5ca1e8e766324ffac2a7dc5c") }
Following is the query to display all documents from a collection with the help of find() method
> db.dateTimeValueLessThanNowDemo.find().pretty();
This will produce the following output
{ "_id" : ObjectId("5ca1e8c166324ffac2a7dc5a"), "CustomerName" : "Mike", "CustomerProductName" : "Product-2", "ArrivalDate" : ISODate("2019-04-01T00:00:00Z") } { "_id" : ObjectId("5ca1e8d266324ffac2a7dc5b"), "CustomerName" : "Chris", "CustomerProductName" : "Product-3", "ArrivalDate" : ISODate("2019-03-31T00:00:00Z") } { "_id" : ObjectId("5ca1e8e766324ffac2a7dc5c"), "CustomerName" : "Robert", "CustomerProductName" : "Product-4", "ArrivalDate" : ISODate("2019-04-02T00:00:00Z") }
Following is the query for datetime value less than NOW. Let’s say the current date is 2019-04-02
> db.dateTimeValueLessThanNowDemo.find({ ArrivalDate: { $lte: new Date() } }).pretty();
This will produce the following output
{ "_id" : ObjectId("5ca1e8ab66324ffac2a7dc59"), "CustomerName" : "Larry", "CustomerProductName" : "Product-1", "ArrivalDate" : ISODate("2017-01-31T00:00:00Z") } { "_id" : ObjectId("5ca1e8c166324ffac2a7dc5a"), "CustomerName" : "Mike", "CustomerProductName" : "Product-2", "ArrivalDate" : ISODate("2019-04-01T00:00:00Z") } { "_id" : ObjectId("5ca1e8d266324ffac2a7dc5b"), "CustomerName" : "Chris", "CustomerProductName" : "Product-3", "ArrivalDate" : ISODate("2019-03-31T00:00:00Z") }
- Related Articles
- Find MongoDB records with Price less than a specific value
- MongoDB query where all array items are less than a specified condition?
- How to set NOW() as default value for datetime datatype in MySQL?
- MongoDB query to match documents with array values greater than a specific value
- MongoDB query to convert the field value and create datetime day of month during projection?
- MySQL query to return all records with a datetime older than 1 week
- Query for documents where array size is greater than 1 in MongoDB?
- MongoDB query for a single field
- Query MongoDB for a nested search
- MongoDB query for documents whose array elements does not have a specific value
- Mask array elements less than a given value in Numpy
- Write a MongoDB query to get nested value?
- Query results that have less than X characters in MySQL?
- How to check whether a column value is less than or greater than a certain value in R?
- Check if any value in an R vector is greater than or less than a certain value.

Advertisements