猫鼬更新不适用于禁用写关注

mongoose update not working with write concern disabled

我想摆脱 "db-operation finished" 上的回调(希望提高 node.js 内部的性能 - 我正在更新几个 100k 文档,节点内存消耗需要很长时间到 1.5GB,一个核心已用尽)。

MongoBooks 是我的 mongoose 模型。

这是带回调的(测试)代码,可以正常工作,它将文档插入空数据库:

MongoBooks.update({ isbn13: "abc" }, { isbn13: "abc", author: "andreas" }, { upsert: true }, function(err, res) {
        console.log(err, res);
    });

这是没有回调的代码和 0 的写关注 - 这是我希望可以提高性能的版本(确实如此)但不对数据库做任何事情(我检查了 mongo repl在 collection):

MongoBooks.update({ isbn13: "abc" }, { isbn13: "abc", author: "andreas" }, { upsert: true, w: 0 });

为什么 w : 0 没有对数据库做任何事情?

根据 Mongoose 3.8 release notes,您必须传递回调或明确告诉 Mongoose 通过传递 true 作为回调参数来执行 "unsafe" 更新。

所以应该是:

MongoBooks.update({ isbn13: "abc" }, 
                  { isbn13: "abc", author: "andreas" }, 
                  { upsert: true, w: 0 },
                  true);