Is there a way to do bulk updates on a collection in mongoose? The strategy I had found used the raw collection driver as follows:
var bulk = Person.collection.initializeOrderedBulkOp();
bulk.find(query).update(update);
...
bulk.execute(callback)
However, bulk
is undefined when I do this. Is it just not supported by mongoose?
Is there a way to do bulk updates on a collection in mongoose? The strategy I had found used the raw collection driver as follows:
var bulk = Person.collection.initializeOrderedBulkOp();
bulk.find(query).update(update);
...
bulk.execute(callback)
However, bulk
is undefined when I do this. Is it just not supported by mongoose?
3 Answers
Reset to default 10NOTE: Modern Mongoose releases support
.bulkWrite()
directly on the Model methods. It is preferable to use this method even in direct implementations of the MongoDB API, since it actually "safely downgrades" to use "individual calls" for the methods supplied in the batch in cases where connecting to a MongoDB version that does not support the "Bulk API" itself.It still calls the same underlying "bulk methods" as described, but rather the software makes the decision how to correctly send to the server than your own code needing to make this determination.
Also note: That Ongoing mongoose releases now require you to
.connect()
in a way that means the connection must be resolved before other actions continue. Using new connection mechanisms ensures that accessors such as.collection
described below are always present when you call them.
You can do it, but the problem is that when accessing the underlying collection object from the base driver the same precautions are not taken as with the implemented mongoose model methods.
All the model methods wrap the underlying methods with other features, but the most common one is making sure that a database connection is open before trying to access the method. This ensures that a Db
instance is present and a Collection()
object can be obtained
Once you use the .collection
accessor on the model, then you are doing it all on your own:
mongoose.connection.on('open',function(err,conn) {
// now it's safe to use
// { .. } Other code
var bulk = Person.collection.initializeOrderedBulkOp();
bulk.find(query).update(update);
bulk.execute(callback)
});
Or some other method that basically ensures the connection has actually been established.
As for native support in for Bulk API methods without diving into the underlying driver level, yes that is being worked on at this present time of writing. But you can still implement it yourself and it will not be breaking code as long as you are connecting to a MongoDB 2.6 server instance or greater.
More detailed info about the query and update query.
var bulk = Person.collection.initializeOrderedBulkOp();
bulk.find(query).update(update);
bulk.execute(function (error) {
callback();
});
Query is searching with array.
Update needs a $set
:
var bulk = Person.collection.initializeOrderedBulkOp();
bulk.find({ '_id': { $in: [] } }).update({ $set: { status: 'active' } });
bulk.execute(function (error) {
callback();
});
Query is a searching the id
var bulk = Person.collection.initializeOrderedBulkOp();
bulk.find({ '_id': id }).update({ $set: { status: 'inactive' } });
bulk.execute(function (error) {
callback();
});
Person.collection.update(
{ '_id': id },
{ $set: { status: 'inactive' } },
{ multi: true },
callback
)
in {'_id': id}
id is the array of ids for record that you want to update, actually it is where clause, you can set your own. in {$set: {status: 'inactive'}}
status is the field that you want to update, you can specify your own fields as key:value pairs. {multi: true}
specify this operation will update multiple records. callback
has the method that will be called after successful update.