Find records on or after a specific date in MongoDB?

MongoDBDatabaseBig Data Analytics

To find records on or after a date, use $gte i.e., greater than equal. Let us create a collection with documents −

> db.demo91.insertOne({"ArrivalDate":new ISODate("2020-01-10")});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5e2d49fd79799acab037af66")
}
> db.demo91.insertOne({"ArrivalDate":new ISODate("2019-12-14")});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5e2d4a0679799acab037af67")
}

Display all documents from a collection with the help of find() method −

> db.demo91.find();

This will produce the following output −

{ "_id" : ObjectId("5e2d49fd79799acab037af66"), "ArrivalDate" : ISODate("2020-01-10T00:00:00Z") }
{ "_id" : ObjectId("5e2d4a0679799acab037af67"), "ArrivalDate" : ISODate("2019-12-14T00:00:00Z") }

Following is the query to find records on or after a specific date in MongoDB −

> db.demo91.find({ArrivalDate: { $gte: ISODate('2020-01-10') } });

This will produce the following output −

{ "_id" : ObjectId("5e2d49fd79799acab037af66"), "ArrivalDate" : ISODate("2020-01-10T00:00:00Z") }
raja
Published on 30-Mar-2020 09:22:40
Advertisements