christkv/node-mongodb-native — Issue #398: collection.update() doesn't always report an error with {safe: true}
Author: jgonera
Created: 2011-11-08 18:25:25+00:00
State: open

There's a problem with collection.update() when using {safe: true}.

In an empty collection we first add a document like this:

col.insert({rev: 0});

Then, we try to increase the rev of a document that does not exist (current `rev` is 0):

col.update({rev: 5}, {$inc: {rev: 1}}, {safe: true}, function(err) {
console.dir(err);
});

I expect this to return an error (no update performed) but it does not...

I think the problem might lie in the way that MongoDB handles this. Let's analyze how it looks in the `mongo` console:

> db.col.insert({rev: 0})
> db.col.find()
{ "_id" : ObjectId("4eb971e8a35518b97e349c66"), "rev" : 0 }

> db.col.update({rev: 0}, {$inc: {rev: 1}})
> db.runCommand("getlasterror")
{
"updatedExisting" : true, <------------- UPDATE SUCCEEDED
"n" : 1,
"connectionId" : 101,
"err" : null, <------------- NO ERROR
"ok" : 1
}

> db.col.find()
{ "_id" : ObjectId("4eb971e8a35518b97e349c66"), "rev" : 1 }

> db.col.update({rev: 5}, {$inc: {rev: 1}})
> db.runCommand("getlasterror")
{
"updatedExisting" : false, <------------- UPDATE FAILED
"n" : 0,
"connectionId" : 101,
"err" : null, <------------- BUT STILL NO ERROR!
"ok" : 1
}

I guess that what the driver sends to the callback is the `err` property without considering the `updatedExisting` property. I think that `updatedExisting` equal to `false` after an update should be also treated as an error, even if this would require a callback with two arguments.