Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoose bulk update operation

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?

like image 708
jtmarmon Avatar asked Jan 05 '15 16:01

jtmarmon


People also ask

How can I update multiple documents in mongoose?

Mongoose | updateMany() Function The updateMany() function is same as update(), except MongoDB will update all documents that match the filter. It is used when the user wants to update all documents according to the condition.

How do I update MongoDB bulk records?

We can update multiple documents from the collection by using the bulk update method in MongoDB. 2) InitializeOrderedBulkOp – This method is used with the bulk update method to update multiple documents from the collection. We need to call this method at the time of using the bulk update method in MongoDB.

What is bulk update?

A bulk update definition specifies a number of conditions and a single update function. A policy must satisfy all the specified conditions in order for it to updated by the function. Bulk updates are executed through a global activity. The bulk update definition code is a parameter of this activity.

Does update call save in mongoose?

When you create an instance of a Mongoose model using new , calling save() makes Mongoose insert a new document. If you load an existing document from the database and modify it, save() updates the existing document instead.


3 Answers

NOTE: 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.

like image 67
Neil Lunn Avatar answered Oct 18 '22 05:10

Neil Lunn


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();                   
});
like image 44
Mangesh Pimpalkar Avatar answered Oct 18 '22 07:10

Mangesh Pimpalkar


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.

like image 1
Farid Bangash Avatar answered Oct 18 '22 07:10

Farid Bangash