Is there any way to access a documents original values when doing updates in a MongoDB collection? I’ve been doing the following:
db.things.find().forEach(function (t) {
t.name = t.name + ' new'; // Appends ' new' to the end of the original value
db.things.save(t);
});
The performance of the above is rather slow (though, I’m dealing with 2.2 million documents), so I was hoping a traditional update() would perform better. I checked the documentation, but I didn’t notice anything that mentions applying part of the original value to the $set value.
You can’t use anything from the object in update. The way you’re doing it with the loop is the best way.