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.
The problem is that my $exists
query is (obviously, on second look) in the wrong place. Documents were being returned that, surely enough, didn't include my_date
.
Here's the patched up function, which works as expected.
var incrementDates = function() {
db.blah.find({ ... , my_date : { $exists : true } ).forEach(function(doc) {
db.blah.update(
{ _id : doc._id }
, { $set : { my_date : new Date(doc.my_date.getTime() + 86400000) }}
);
});
}