Inserting Date() in MongoDB through Mongo shell?

In order to insert Date() in MongoDB through Mongo shell, use the following syntax

var yourVariableName= new Date(year,month, day, hour, minute);

Let us first create a date variable

> var creatingDate = new Date(2019, 03, 29, 13, 12);

Let us create a collection with documents:


Following is the query to display all documents from a collection with the help of find() method

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

This will produce the following output

   "_id" : ObjectId("5c9d1b19a629b87623db1b21"),
   "UserName" : "John",
   "UserMessages" : [
   "UserPostDate" : ISODate("2019-04-29T07:42:00Z")