Friday 4 June 2021

MongoDB: set the field with current timestamp in MongoDB

‘Date.now()’ function is used to set the field with current timestamp.

 

Example

db.audit.insert({"type" : "login", "client" : "mobile", "userId" : 123, "createdTimestamp": Date.now()})

 

Find the below snippet.

> db.audit.insert({"type" : "login", "client" : "mobile", "userId" : 123, "createdTimestamp": Date.now()})
WriteResult({ "nInserted" : 1 })
> 
> db.audit.find().pretty()
{
      "_id" : ObjectId("60bb0156baf44d88348459c4"),
      "type" : "login",
      "client" : "mobile",
      "userId" : 123,
      "createdTimestamp" : 1622868310979
}

 

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment