2014-07-02 28 views
1

我正在使用Mongo的本地节点驱动程序。对于一个upsert如:对于Mongo节点驱动程序的插件,是否插入或更新?

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

有没有办法确定upsert做了插入还是更新?使用Mongo shell,您可以返回WriteResult.nUpserted来确定这一点,但我不确定如何从节点本机驱动程序获取该信息。 http://docs.mongodb.org/manual/reference/method/WriteResult/#WriteResult.nUpserted

谢谢。

回答

1

您应该能够通过检查传递给你的回调函数的第三个参数,找出:

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) 
});