I have a big collection in MongoDB: size - 94.605.081.327 B
count - 54.738.234
. I have to change all documents in this collection, and it can't be done in a single update
.
I would like to stream the data and update the collection one document at a time. Another developer on the same project recommended an approach like this:
stream.on('data', (data)=>{
stream.pause();
data.field = 'newValue'; // update
data.save((err)=>{
stream.resume();
})
})
Is it a good idea? Is there a more efficient way of doing this update
with Node.js
and Mongoose
?
You can use bulk operation
. Check out MongoDB documentation for detailed information. If you need to involve mongoose
, your code would look something like this:
var bulk = Items.collection.initializeOrderedBulkOp();
bulk.find(query).update(update);
bulk.execute(function(error) {
callback();
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.