最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Mongoose bulk update operation - Stack Overflow

programmeradmin1浏览0评论

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?

Share Improve this question edited Jun 29, 2017 at 4:32 Neil Lunn 151k36 gold badges354 silver badges324 bronze badges asked Jan 5, 2015 at 16:19 jtmarmonjtmarmon 6,1798 gold badges29 silver badges46 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 10

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.

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.

发布评论

评论列表(0)

  1. 暂无评论