Mongoose批量更新操作 [英] Mongoose bulk update operation

查看:816
本文介绍了Mongoose批量更新操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在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)

然而,<$我执行此操作时未定义c $ c> bulk 。是不是mongoose不支持?

However, bulk is undefined when I do this. Is it just not supported by mongoose?

推荐答案


注意:现代Mongoose版本支持 .bulkWrite() 直接在Model方法上。即使在MongoDB API的直接实现中也最好使用此方法,因为在连接到不支持不支持的MongoDB版本的情况下,它实际上安全地降级以使用批处理中提供的方法的单独调用。批量API本身。

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.

另请注意:现在正在进行的mongoose版本需要您 .connect() 这意味着在继续其他操作之前必须解析连接 。使用新的连接机制可确保在调用它们时始终存在下面描述的访问者,例如 .collection

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.






你可以这样做,但问题是当从基本驱动程序访问底层集合对象时,不会采用与实现的mongoose模型相同的预防措施方法。


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.

所有模型方法都使用其他功能包装底层方法,但最常见的方法是在尝试访问方法之前确保数据库连接已打开。这可以确保存在 Db 实例,并且可以获得 Collection()对象

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

在模型上使用 .collection 访问器后,您就可以自己完成所有操作了:

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.

对于批量API方法的原生支持而没有潜入底层驱动程序级别,是的,正是在本文撰写时正在进行的工作。但是你仍然可以自己实现它,只要你连接到MongoDB 2.6服务器实例或更高版本,它就不会破坏代码。

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.

这篇关于Mongoose批量更新操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆