2

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.+

2

1 Answer 1

3

You can try this.

db.testDBA.updateOne(
   {},
   {
     $currentDate: {
        modifiedOn:true
     },
     $set: {
        "item":"anything"
     }
   })

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

enter image description here

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

enter image description here

1
  • it worked. thanks
    – Racer SQL
    Commented Feb 2, 2021 at 14:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.