In updateMany query I want to update date


I have try both this ways but getting error: Cast to date failed

Try enclosing your { "$set" : … } inside brackets like [ { "$set" : … } ] to do update using aggregation.

mongosh> c.find()
{ _id: 0, start: 2022-04-09T12:44:46.561Z, duration: 5 }

mongosh> c.updateOne( {} ,
  [ { "$set" : {
      "end" : { "$dateAdd" : { startDate: "$start", unit:"minute" , amount:"$duration" }
    }
  } } ]
)
{ acknowledged: true,
  insertedId: null,
  matchedCount: 1,
  modifiedCount: 1,
  upsertedCount: 0 }

mongosh> c.find()
{ _id: 0,
  start: 2022-04-09T12:44:46.561Z,
  duration: 5,
  end: 2022-04-09T12:49:46.561Z }

Next time your need to publish code or document, please read Formatting code and log snippets in posts and then publish as text rather than screenshot so that we can cut-n-paste otherwise we have to manually type what ever you publish before we can experiment.

1 Like

Thank you @steevej for your help it’s working.
And I will keep that in mind from now onwards.

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.