简体   繁体   English

Mongoose 批量更新操作

[英]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.但是,当我这样做时, bulk是未定义的。 Is it just not supported by mongoose?猫鼬不支持它吗?

NOTE : Modern Mongoose releases support.bulkWrite() directly on the Model methods.注意:现代猫鼬版本直接在模型方法上支持.bulkWrite() 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.即使在 MongoDB API 的直接实现中也最好使用此方法,因为在连接到不支持“批量 API”本身。

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.另请注意:正在进行的猫鼬版本现在要求您以某种方式.connect() ,这意味着必须在其他操作继续之前解析连接。 Using new connection mechanisms ensures that accessors such as .collection described below are always present when you call them.使用新的连接机制可确保在您调用它们时始终存在诸如下面描述的.collection之类的访问器。


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这确保存在Db实例并且可以获得Collection()对象

Once you use the .collection accessor on the model, then you are doing it all on your own:在模型上使用.collection访问器后,您就可以自己完成所有操作了:

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.至于在不深入底层驱动程序级别的情况下对 Bulk API 方法的本机支持,是的,目前正在研究中。 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.但是您仍然可以自己实现它,只要您连接到 MongoDB 2.6 服务器实例或更高版本,它就不会破坏代码。

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 :更新需要一个$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查询是搜索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.{'_id': id} id 是你要更新的记录的 id 数组,实际上是 where 子句,你可以自己设置。 in {$set: {status: 'inactive'}} status is the field that you want to update, you can specify your own fields as key:value pairs.{$set: {status: 'inactive'}} status 是您要更新的字段,您可以将自己的字段指定为键:值对。 {multi: true} specify this operation will update multiple records. {multi: true}指定此操作将更新多条记录。 callback has the method that will be called after successful update. callback具有更新成功后将调用的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM