
- 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
Get documents expired before today in MongoDB?
You can use $lte operator along with Date() for this. Let us first create a collection with documents. Here, we have set the date 2019-05-11, which is the current date −
> db.getDocumentsExpiredDemo.insertOne({"ArrivalDate":new ISODate("2019-05-11")}); { "acknowledged" : true, "insertedId" : ObjectId("5cd563b17924bb85b3f4893b") } > db.getDocumentsExpiredDemo.insertOne({"ArrivalDate":new ISODate("2019-01-01")}); { "acknowledged" : true, "insertedId" : ObjectId("5cd563bf7924bb85b3f4893c") } > db.getDocumentsExpiredDemo.insertOne({"ArrivalDate":new ISODate("2019-05-10")}); { "acknowledged" : true, "insertedId" : ObjectId("5cd563ca7924bb85b3f4893d") } > db.getDocumentsExpiredDemo.insertOne({"ArrivalDate":new ISODate("2019-02-01")}); { "acknowledged" : true, "insertedId" : ObjectId("5cd563e77924bb85b3f4893e") }
Following is the query to display all documents from a collection with the help of find() method −
> db.getDocumentsExpiredDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cd563b17924bb85b3f4893b"), "ArrivalDate" : ISODate("2019-05-11T00:00:00Z") } { "_id" : ObjectId("5cd563bf7924bb85b3f4893c"), "ArrivalDate" : ISODate("2019-01-01T00:00:00Z") } { "_id" : ObjectId("5cd563ca7924bb85b3f4893d"), "ArrivalDate" : ISODate("2019-05-10T00:00:00Z") } { "_id" : ObjectId("5cd563e77924bb85b3f4893e"), "ArrivalDate" : ISODate("2019-02-01T00:00:00Z") }
Following is the query to get documents expired before today in MongoDB −
> db.getDocumentsExpiredDemo.find({ "ArrivalDate": { $lte : new Date()}});
This will produce the following output −
{ "_id" : ObjectId("5cd563bf7924bb85b3f4893c"), "ArrivalDate" : ISODate("2019-01-01T00:00:00Z") } { "_id" : ObjectId("5cd563ca7924bb85b3f4893d"), "ArrivalDate" : ISODate("2019-05-10T00:00:00Z") } { "_id" : ObjectId("5cd563e77924bb85b3f4893e"), "ArrivalDate" : ISODate("2019-02-01T00:00:00Z") }
- Related Articles
- How to validate documents before insert or update in MongoDB?
- Get number of updated documents in MongoDB?
- How to get documents by tags in MongoDB?
- What to check before throwing the expired medicine away?
- Get MongoDB documents that contains specific attributes in array
- Selecting records 15 days before today in MySQL?
- MongoDB query to get distinct FirstName values from documents
- Get MongoDB documents with max attribute per group in a collection?
- Get the size of all the documents in a MongoDB query?
- Get all embedded documents with “isMarried” status in a MongoDB collection
- MongoDB query to get only specific fields in nested array documents?
- MongoDB query to get documents with multiple conditions set in $or?
- Check for existing documents/embedded documents in MongoDB
- MongoDB aggregation to get two documents with the least marks
- Get the count of the number of documents in a MongoDB Collection?

Advertisements