Skip to content

Instantly share code, notes, and snippets.

@nucknorris
Created July 2, 2013 10:41
Show Gist options
  • Save nucknorris/5908333 to your computer and use it in GitHub Desktop.
Save nucknorris/5908333 to your computer and use it in GitHub Desktop.
MongoDB Upsert with increment
final BasicDBObject query = new BasicDBObject();
List<BasicDBObject> obj = new ArrayList<BasicDBObject>();
obj.add(new BasicDBObject("name", person.getName()));
obj.add(new BasicDBObject("age", person.getAge()));
query.put("$and", obj);
BasicDBObject increment = new BasicDBObject();
increment.put("$inc", new BasicDBObject().append("quantity", 1));
increment.put("$set", new BasicDBObject("person", person));
conversionCollection.update(query, increment, true /*upsert*/ , false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment