2014-07-02 29 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) 
});