Is there any Equivalent of NOW() in MongoDB

Viewed 25977

I am trying to migrate from MySQL to MongoDB and want to insert the current date as I do with NOW() in MySQL. Does anyone know a 1 line solution or should I do it with PHP?

4 Answers

For new features in MongoDB 4.2. You can use NOW to get the current datetime value.

Use Case: to access the variable, prefix with $$ and enclose in quotes.

db.students.updateOne( { _id: 3 }, [ { $set: { "test3": 98, modified: "$$NOW"} } ] )

Moreover, The CLUSTER_TIME will return the current timestamp value but is only available on replica sets and sharded clusters.

Related