Last active
December 22, 2015 21:09
-
-
Save izmailoff/6531743 to your computer and use it in GitHub Desktop.
a way to upsert with condition
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// You just need to ignore dup key errors :) | |
// in Mongo shell: ... | |
> | |
> var lastUpdateTime = ISODate("2013-09-10") | |
> var newUpdateTime = ISODate("2013-09-12") | |
> | |
> lastUpdateTime | |
ISODate("2013-09-10T00:00:00Z") | |
> newUpdateTime | |
ISODate("2013-09-12T00:00:00Z") | |
> | |
> var id = new ObjectId() | |
> id | |
ObjectId("52310502f3bf4823f81e7fc9") | |
> | |
> // collection is empty, first update will do insert: | |
> db.testcol.update( | |
... {"_id" : id, "ts" : { $lt : lastUpdateTime } }, | |
... { $set: { ts: lastUpdateTime, data: 123 } }, | |
... { upsert: true, multi: false } | |
... ); | |
> | |
> db.testcol.find() | |
{ "_id" : ObjectId("52310502f3bf4823f81e7fc9"), "data" : 123, "ts" : ISODate("2013-09-10T00:00:00Z") } | |
> | |
> // try one more time to check that nothing happens (due to error): | |
> db.testcol.update( | |
... {"_id" : id, "ts" : { $lt : lastUpdateTime } }, | |
... { $set: { ts: lastUpdateTime, data: 123 } }, | |
... { upsert: true, multi: false } | |
... ); | |
E11000 duplicate key error index: test.testcol.$_id_ dup key: { : ObjectId('52310502f3bf4823f81e7fc9') } | |
> | |
> var tooOldToUpdate = ISODate("2013-09-09") | |
> | |
> // update does not happen because query condition does not match | |
> // and mongo tries to insert with the same id (and fails with dup again): | |
> db.testcol.update( | |
... {"_id" : id, "ts" : { $lt : tooOldToUpdate } }, | |
... { $set: { ts: tooOldToUpdate, data: 999 } }, | |
... { upsert: true, multi: false } | |
... ); | |
E11000 duplicate key error index: test.testcol.$_id_ dup key: { : ObjectId('52310502f3bf4823f81e7fc9') } | |
> | |
> // now query cond actually matches, so update rather than insert happens which works | |
> // as expected: | |
> db.testcol.update( | |
... {"_id" : id, "ts" : { $lt : newUpdateTime } }, | |
... { $set: { ts: newUpdateTime, data: 999 } }, | |
... { upsert: true, multi: false } | |
... ); | |
> | |
> // check that everything worked: | |
> db.testcol.find() | |
{ "_id" : ObjectId("52310502f3bf4823f81e7fc9"), "data" : 999, "ts" : ISODate("2013-09-12T00:00:00Z") } | |
> | |
// The only annoying part are those errors, but they are cheap and safe, so you are good. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment