简体   繁体   English

Mongoose 4.x“model.update()”回调发生了变化

[英]Mongoose 4.x “model.update()” callback changed

Before mongoose 4.x, in update(), you can check the 2nd parameter in callback to see if the document is found. 在mongoose 4.x之前,在update()中,您可以检查回调中的第二个参数以查看是否找到了文档。 In example below, you can use "rowAffected" to see if there exists a document with username john. 在下面的示例中,您可以使用“rowAffected”查看是否存在包含用户名john的文档。

model.update({ username: "john" }, { ... }, function(err, rowAffected){ 
    if (rowAffected) // document found

But now from mongoose 4.x, the 2nd parameter in callback becomes the raw output of MongoDB from update operation. 但是现在从mongoose 4.x开始,回调中的第二个参数成为MongoDB从更新操作的原始输出。 So to find if document exists I have to do raw.n 因此,要查找文档是否存在,我必须执行raw.n.

model.update({ username: "john" }, { ... }, function(err, raw){ 
    if (raw.n) // document found

My question is are "rowAffected" and "raw.n" the same thing ? 我的问题是“rowAffected”和“raw.n”是一回事吗? If so, is it safe to replace all rowAffected by raw.n when migrating from 3.x to 4.x? 如果是这样,当从3.x迁移到4.x时,将raw.n替换为raw.n的所有rowA是否安全?

Yes, they are the same thing. 是的,他们是一回事。 According to the 4.0 release notes : 根据4.0版本说明

#2552 : Upgraded mongodb driver to 2.0.x. #2552 :将mongodb驱动程序升级到2.0.x. Mongoose is a wrapper layer on top of the MongoDB node driver. Mongoose是MongoDB节点驱动程序之上的包装层。 The mongodb driver recently released version 2.0, which includes numerous performance and usability improvements. mongodb驱动程序最近发布了2.0版,其中包括许多性能和可用性改进。 The new driver, however, introduces a few changes that affect the way you use Mongoose: 但是,新驱动程序引入了一些影响Mongoose使用方式的更改:

  • If you are connecting to a replica set, you must specify the replicaSet option in the connection string. 如果要连接到副本集,则必须在连接字符串中指定replicaSet选项。
  • update returns a result object from the MongoDB server, rather than just the number affected. update 从MongoDB服务器返回结果对象,而不仅仅是受影响的数字。 The second parameter to the callback will now look like { ok: 1, n: 3 } rather than simply the number affected. 回调的第二个参数现在看起来像{ ok: 1, n: 3 }而不仅仅是受影响的数字。

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

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