node.js - 使用 Mongo Node 驱动程序更新插入时,它是插入还是更新?

标签 node.js mongodb node-mongodb-native

我正在使用 Mongo 的 native Node 驱动程序。对于像这样的更新插入:

collection.update(query, setData, { upsert: true }, callback);

有没有办法确定 upsert 是否进行了插入或更新?使用 Mongo shell 你可以返回 WriteResult.nUpserted以确定这一点,但我不确定如何从 Node native 驱动程序获取该信息。 http://docs.mongodb.org/manual/reference/method/WriteResult/#WriteResult.nUpserted

谢谢。

最佳答案

您应该能够通过检查传递给回调的第三个参数来找出答案:

collection.update(query, setData, {upsert: true}, function(err, nAffected, raw) {
  if (err) throw err;
  console.dir(raw);
  // raw will contain updatedExisting and the inserted item _id (if applicable)
});

关于node.js - 使用 Mongo Node 驱动程序更新插入时,它是插入还是更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24539674/

相关文章:

node.js - 带参数的快速路由,后跟不带参数的路由

node.js - 在 Node.JS 中使用 Passport 注册(注册)

ruby-on-rails - 如何使用 Mongoid 执行 runCommand?

node.js - 将缓存添加到 node.js MongoDB API?

node.js - 使用 nodejs 管理第三方库(不是 Node 模块)?

node.js - 我需要为 MongoDB 启用身份验证吗?

node.js - 用于保存任何数据的 Mongodb 动态模式

node.js - Mongodb 可尾游标 { 错误 : No More Documents in Tailed Cursor }

node.js - 将扩展 JSON 与 node.js mongodb native 驱动程序一起使用

node.js - MongoClient 和 MongoClient.connect() 方法回调中得到的客户端对象有什么区别