Not being a particularly strong Javascript guy, I\'m having a bit of trouble trying to update a lot of Date objects in Mongo.
It seems that $inc has not
Starting in Mongo 4.2, db.collection.update() can accept an aggregation pipeline, finally allowing the update of a field based on its own value; thus avoiding inefficient find/foreach patterns.
Also, you were looking at the $inc operator to add a day, but now that we can use an aggregation pipeline as an update, the $add operator can be used:
// { "date" : ISODate("2020-04-05T07:14:17.802Z"), "x" : "y" }
db.collection.updateMany(
{ date : { $exists : true } },
[{ $set: { date: { $add: ["$date", 24*60*60000] } } }]
)
// { "date" : ISODate("2020-04-06T07:14:17.802Z"), "x" : "y" }
The first part { date : { $exists : true } } is the match query, filtering which documents to update (in our case all documents having the date field).
The second part [{ $set: { date: { $add: ["$date", 24*60*60000] } } }] is the update aggregation pipeline (note the squared brackets signifying the use of an aggregation pipeline). $set is a new aggregation operator and an alias of $addFields. Then any aggregation operator can be used within the $set stage; in our case a simple $addition between the existing date and the representation of a day in milliseconds.