Skip to content

Instantly share code, notes, and snippets.

@alcaeus
Created May 22, 2017 13:01
Show Gist options
  • Save alcaeus/4509c4479866d51103f03f5d755f5d4f to your computer and use it in GitHub Desktop.
Save alcaeus/4509c4479866d51103f03f5d755f5d4f to your computer and use it in GitHub Desktop.
MongoDB findAndModify
> db.foo.count()
> db.foo.findAndModify({query: { foo: 'bar' }, update: {foo: 'bar'}, upsert: true})
null
> db.foo.findAndModify({query: { foo: 'bar' }, update: {foo: 'baz'}, upsert: true})
{ "_id" : ObjectId("5922e0b18c00e100490e9108"), "foo" : "bar" }
> db.foo.findAndModify({query: { foo: 'bar' }, update: {foo: 'foobar'}, upsert: true, new: true})
{ "_id" : ObjectId("5922e0b18c00e100490e9108"), "foo" : "foobar" }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment