Skip to content

Instantly share code, notes, and snippets.

@mdirolf
Created July 8, 2010 21:59
Show Gist options
  • Save mdirolf/468712 to your computer and use it in GitHub Desktop.
Save mdirolf/468712 to your computer and use it in GitHub Desktop.
MongoDB insert if not present
> db.test.find()
> db.test.update({foo: 1, bar: 2}, {$set: {}}, true);
> db.test.find()
{ "_id" : ObjectId("4c3649f8dd24e71e36ca6f69"), "bar" : 2, "foo" : 1 }
> db.test.update({foo: 1, bar: 2}, {$set: {}}, true);
> db.test.find()
{ "_id" : ObjectId("4c3649f8dd24e71e36ca6f69"), "bar" : 2, "foo" : 1 }
> db.test.update({foo: 1}, {$set: {}}, true);
> db.test.find()
{ "_id" : ObjectId("4c3649f8dd24e71e36ca6f69"), "bar" : 2, "foo" : 1 }
> db.test.update({foo: 1, bar: 3}, {$set: {}}, true);
> db.test.find()
{ "_id" : ObjectId("4c3649f8dd24e71e36ca6f69"), "bar" : 2, "foo" : 1 }
{ "_id" : ObjectId("4c364a0bdd24e71e36ca6f6a"), "bar" : 3, "foo" : 1 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment