How to work Date query with ISODate in MongoDB?

MongoDBDatabaseBig Data Analytics

Use $gte operator along with ISODate() to work Date query with ISODate in MongoDB.

To understand the concept, let us create a collection with the document. The query to create a collection with a document is as follows −

> db.dateDemo.insertOne({"StudentName":"John","StudentAge":26,"AdmissionDate":new ISODate("2013-06-07")});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c8a65799064dcd4a68b70ea")
}

Display all documents from a collection with the help of find() method. The query is as follows −

> db.dateDemo.find().pretty();

The following is the output −

{
   "_id" : ObjectId("5c8a65799064dcd4a68b70ea"),
   "StudentName" : "John",
   "StudentAge" : 26,
   "AdmissionDate" : ISODate("2013-06-07T00:00:00Z")
}

Here is the date query with ISODate in MongoDB −

> db.dateDemo.find({"AdmissionDate":{"$gte": ISODate("2013-06-07T00:00:00Z")}}).pretty();

The following is the output −

{
   "_id" : ObjectId("5c8a65799064dcd4a68b70ea"),
   "StudentName" : "John",
   "StudentAge" : 26,
   "AdmissionDate" : ISODate("2013-06-07T00:00:00Z")
}
raja
Published on 29-Mar-2019 14:39:12
Advertisements