Failure to update twice the same document

Hi there,

If I do 2 updates on the same document and use document as selector, the second will fail. But it will work if {_id:document._id} is used as a selector instead.


I understand what is wrong, but I’d like to understand why the first query would be ok and not the second one.

Can someone explain what’s going on ?

I dont see any update failing, just 1 of them does not match selector, cause it should not (using same “status” object in 2 updates but you change the document in the time between)

first argument for Collection.update is selector. In your test1 selector is a whole object, means to update a document where all keys are equals with values. Then you call update again. But your document is already changed! Means no document would be found. see next example:

var status = Status.findOne(); 
// status is {_id: '1111', foo: true}

var a = Status.update(status, {$set: {foo: true}}); 
// will be updated document where _id is '111' and foo is true
// after update the document is {_id: '1111', foo: false}

var b = Status.update(status, {$set: {foo: true}}); 
// tryes to updat document where _id is '111' and foo is true, but it will fail cause you just set false for foo field

I don’t know why, I strongly expected some trick that would take the object’s _id as a selector, and then never considered that the object could be used as a normal mongo selector on every property…

Makes perfect sense now, thanks !