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({
"field.field":"zzzzzzzz"
},{$set:{
"field2" : {
"field3" : ObjectId("5d4dc2c2b3e6de001b934ec6"),
"field4" : "xxxxxxxxxxxxxxxxxxxxxxxxxx"
},
"DateOfModification: "$$NOW"
}
}})
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: {"item":"anything",$currentDate: {"modifiedOn":true}}
})
'The dollar ($) prefixed field '$currentDate' in '$currentDate' is not valid for storage.+
$$NOW
is available only in Aggregation Pipeline, not in simpleupdateMany
. Trynew Date()
orISODate()
. You can use aggregation pipelines inupdateMany
, however it also requires MongoDB 4.2 or newer.