@Override public Object execute(StatementRuntime runtime) { Method m = runtime.getMethod(); Update update = m.getAnnotation(Update.class); String doc = update.coll(); String cnd = update.cnd(); String with = update.with(); boolean upsert = update.upsert(); boolean multi = update.multi(); int queryParasNum = StringUtils.countMatches(cnd, "#"); int withParasNum = StringUtils.countMatches(with, "#"); List<Object> paras = runtime.getParameters(); if (paras.size() < queryParasNum + withParasNum) { return null; } List<Object> queryParas = paras.subList(0, queryParasNum); List<Object> withParas = paras.subList(queryParasNum, queryParasNum + withParasNum); MongoCollection mc = this.jongo.getCollection(doc); org.jongo.Update upder = null; if (cnd.equals("")) { upder = mc.update(cnd); } else if (cnd.equals("#oid")) { upder = mc.update(new ObjectId(paras.get(0).toString())); } else if (cnd.equals("#id")) { upder = mc.update("{_id : #}", paras.get(0)); } else if (queryParasNum > 0) { upder = mc.update(cnd, queryParas.toArray()); } else { upder = mc.update(cnd); } if (upsert) { upder = upder.upsert(); } if (multi) { upder = upder.multi(); } WriteResult wr = null; if (with.equals("")) { wr = upder.with(""); } else if (withParasNum > 0) { wr = upder.with(with, withParas.toArray()); } else { wr = upder.with(with); } return wr; }
@Test // https://groups.google.com/forum/?hl=fr&fromgroups#!topic/jongo-user/ga3n5_ybYm4 public void shouldBindAListOfPojo() throws Exception { Buddies buddies = new Buddies(); buddies.add(new Friend("john")); collection.save(buddies); collection.update("{}").with("{$push:{friends:#}}", new Friend("peter")); assertThat(collection.count("{ friends.name : 'peter'}")).isEqualTo(1); }
public static String generateUniqueId(String type) { MongoCollection idSeed = MongoDBController.getCollection(CollectionNames.idcollection); ID id = idSeed.findOne().as(ID.class); int uniqueNumber = id.val.intValue(); id.val = new Integer(uniqueNumber + 1); idSeed.update("{val: #}", uniqueNumber).upsert().with("{val : #}", (uniqueNumber + 1)); // idSeed.update().with("{val : " + id.val + "}"); DateTime now = DateTime.now(); String uniqueId = now.year().getAsString() + now.monthOfYear().getAsString() + type + uniqueNumber; return uniqueId; }
@BodyParser.Of(BodyParser.Json.class) public static Result updateBeer(String id) { // TODO: Fix imgId so we can updated image w/ this as well. beers.update(new ObjectId(id)).with(request().body().asJson().toString()); return ok(); }