Search code examples
mongodbdatetimemongodb-querymongodb-update

Specify date format when updating date field in mongoDB document


I am using mongoDB 3.4 and want to close documents in a mongoDB collection by setting an EffectiveEndDateTime field to todays/current date time and want it in this format: yyyy-MM-dd HH:mm:ss.SSS.

How can I specify the date format?

My current update query is:

db.stores.update({
    "storeID": "P0925"
}, {
    $set: {
        "EffectiveEndDateTime": Date()
    }
}, {
    "multi": true
})

Thanks


Solution

  • Just add new keyword to Date in your query

    db.stores.update({"storeID" : "P0925"}, {$set : {"EffectiveEndDateTime": new Date()}}, {"multi" : true})