我正在使用 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/