What is the getdate() equivalent for MongoDB? I have a query to update and set some values in some fields.
Obut Now I need to update these field and in adition, I need to set DateOfModification to the current date and time.
What is the equivalent to GETDATE() from sql server to use in a $set?
I tried this:
db.col.updateMany({
     &quot;field.field&quot;:&quot;zzzzzzzz&quot; 
},{$set:{
        &quot;field2&quot; : {
        &quot;field3&quot; : ObjectId(&quot;5d4dc2c2b3e6de001b934ec6&quot;), 
        &quot;field4&quot; : &quot;xxxxxxxxxxxxxxxxxxxxxxxxxx&quot;
        },
        &quot;DateOfModification: &quot;$$NOW&quot;
    }
}})

but It seems the $$NOW only works with mongoDB 4.2 and I'm still using 4.0
Thanks
Edit:
I tried $currentDate but still getting errors:
db.testeDBA.update(
        {},
          {$set: {&quot;item&quot;:&quot;anything&quot;,$currentDate: {&quot;modifiedOn&quot;:true}}      
        })
        


'The dollar ($) prefixed field '$currentDate' in
'$currentDate' is not valid for storage.+
You can try this.
db.testDBA.updateOne(
   {},
   {
     $currentDate: {
        modifiedOn:true
     },
     $set: {
        &quot;item&quot;:&quot;anything&quot;
     }
   })

I inserted a new document and updated it. It worked.

If I run this code again, the document will be updated like this