private void doUpdate(MongoCollection collection, String opQuery, Object[] parameters, String modifier, boolean upsert, boolean multi) throws DataServiceFault { if (opQuery != null) { if (parameters.length > 0) { Update update = collection.update(opQuery); if (upsert) { update = update.upsert(); } if (multi) { update = update.multi(); } update.with(modifier, parameters); } else { Update update = collection.update(opQuery); if (upsert) { update = update.upsert(); } if (multi) { update = update.multi(); } update.with(modifier); } } else { throw new DataServiceFault("Mongo update statements must contain a query"); } }
WriteResult update = jongo.getCollection(tableName).update(new ObjectId(newPropertyObjectKeyValue)).with(query); int wasUpdated = update.getN(); if (wasUpdated == 1) {
WriteResult update = jongo.getCollection(tableName).update(new ObjectId(oldPropertyObjectKeyValue)).with(query); int wasUpdated = update.getN(); if (wasUpdated == 1) {
public void update(final String id, final String modifier, final boolean multi, final boolean upsert, final Object... params) throws MongoDataException { try { Update update = getCollection().update(new ObjectId(id)); if (multi) { update.multi(); } if (upsert) { update.upsert(); } update.with(modifier, params); } catch (com.mongodb.DuplicateKeyException ex) { String msg = "Duplicate key for update with id='" + id + "', modifier=" + modifier + ", multi=" + multi + ", upsert=" + upsert + ", params" + Arrays.toString(params); log.error(msg, ex); throw new MongoDataException(msg, ex); } catch (MongoException ex) { String msg = "Unable to do update with id='" + id + "', modifier=" + modifier + ", multi=" + multi + ", upsert=" + upsert + ", params" + Arrays.toString(params); log.error(msg, ex); throw new MongoDataException(msg, ex); } }
/** * updating the entire properties object at the same time, with force flag * to allow overwriting of updates * from other instances of the state * * @param force * @throws UpdateConflictException * | will not throw anything when $force flag is true */ private synchronized boolean updateProperties(final boolean force) throws UpdateConflictException { final Long now = System.nanoTime(); /* write to database */ final WriteResult result = (force) ? collection.update("{_id: #}", getId()).with("{$set: {properties: #, timestamp: #}}", properties, now) : collection.update("{_id: #, timestamp: #}", getId(), timestamp).with( "{$set: {properties: #, timestamp: #}}", properties, now); /* check results */ final Boolean updatedExisting = (Boolean) result .getField("updatedExisting"); if (result.getN() == 0 && result.getError() == null) { throw new UpdateConflictException(timestamp); } else if (result.getN() != 1) { throw new MongoException(result.getError()+ " <--- "+properties); } timestamp = now; return updatedExisting; }
@Override public void update(final String id, final String modifier, final boolean multi, final boolean upsert) throws MongoDataException { try { Update update = getCollection().update(new ObjectId(id)); if (multi) { update.multi(); } if (upsert) { update.upsert(); } update.with(modifier); } catch (com.mongodb.DuplicateKeyException ex) { String msg = "Duplicate key for update with id='" + id + "', modifier=" + modifier + ", multi=" + multi + ", upsert=" + upsert; log.error(msg, ex); throw new MongoDataException(msg, ex); } catch (MongoException ex) { String msg = "Unable to do update with id='" + id + "', modifier=" + modifier + ", multi=" + multi + ", upsert=" + upsert; log.error(msg, ex); throw new MongoDataException(msg, ex); } }
@Override public void update(final String id, final Object updateObject, final boolean multi, final boolean upsert) throws MongoDataException { try { Update update = getCollection().update(new ObjectId(id)); if (multi) { update.multi(); } if (upsert) { update.upsert(); } update.with(updateObject); } catch (com.mongodb.DuplicateKeyException ex) { String msg = "Duplicate key for update with id='" + id + "', updatedObject=" + updateObject + ", multi=" + multi + ", upsert=" + upsert; log.error(msg, ex); throw new MongoDataException(msg, ex); } catch (MongoException ex) { String msg = "Unable to do update with id='" + id + "', updatedObject=" + updateObject + ", multi=" + multi + ", upsert=" + upsert; log.error(msg, ex); throw new MongoDataException(msg, ex); } }