/** * Calls {@link DB#doEval(java.lang.String, java.lang.Object[]) }. If the command is successful, the "retval" field is extracted and * returned. Otherwise an exception is thrown. * * @param code {@code String} representation of JavaScript function * @param args arguments to pass to the JavaScript function * @return result of the execution * @throws MongoException if the operation failed * @mongodb.driver.manual reference/method/db.eval/ db.eval() * @deprecated The eval command was deprecated in MongoDB 3.0 */ @Deprecated public Object eval(final String code, final Object... args) { CommandResult result = doEval(code, args); result.throwOnError(); return result.get("retval"); }
void process(final MappedClass mc, final Validation validation) { if (validation != null) { String collectionName = mc.getCollectionName(); CommandResult result = getDB() .command(new BasicDBObject("collMod", collectionName) .append("validator", parse(validation.value())) .append("validationLevel", validation.level().getValue()) .append("validationAction", validation.action().getValue()) ); if (!result.ok()) { if (result.getInt("code") == 26) { ValidationOptions options = new ValidationOptions() .validator(parse(validation.value())) .validationLevel(validation.level()) .validationAction(validation.action()); getDatabase().createCollection(collectionName, new CreateCollectionOptions().validationOptions(options)); } else { result.throwOnError(); } } } }
DB db = client.getDB("$external"); CommandResult result = db.command(new BasicDBObject("createUser",userName).append("roles", Collections.singletonList(new BasicDBObject("role", "dbOwner").append("db", "CMRepWs_Username")))); result.throwOnError();
/** * Calls {@link DB#doEval(java.lang.String, java.lang.Object[]) }. If the command is successful, the "retval" field is extracted and * returned. Otherwise an exception is thrown. * * @param code {@code String} representation of JavaScript function * @param args arguments to pass to the JavaScript function * @return result of the execution * @throws MongoException if the operation failed * @mongodb.driver.manual reference/method/db.eval/ db.eval() * @deprecated The eval command was deprecated in MongoDB 3.0 */ @Deprecated public Object eval(final String code, final Object... args) { CommandResult result = doEval(code, args); result.throwOnError(); return result.get("retval"); }
public static void shutdown(String host, int port) { MongoClient mongo = null; try { mongo = new MongoClient(host, port); DB db = mongo.getDB("admin"); CommandResult shutdownResult = db.command(new BasicDBObject( "shutdown", 1)); shutdownResult.throwOnError(); } catch (MongoException e) { //It is ok because response could not be returned because network connection is closed. } catch (Throwable e) { throw new IllegalStateException("Mongodb could not be shutdown.", e); } finally { mongo.close(); } } }
protected static void errorResult(DBCollection coll, int code, String err) { ((FongoDB) coll.getDB()).notOkErrorResult(code, err).throwOnError(); }
public static void shutdown(String host, int port) { MongoClient mongo = null; try { mongo = new MongoClient(host, port); DB db = mongo.getDB("admin"); CommandResult shutdownResult = db.command(new BasicDBObject( "shutdown", 1)); shutdownResult.throwOnError(); } catch (MongoException e) { //It is ok because response could not be returned because network connection is closed. } catch (Throwable e) { throw new IllegalStateException("Mongodb could not be shutdown.", e); } finally { mongo.close(); } } }
public synchronized List<DBObject> geoNear(DBObject near, DBObject query, Number limit, Number maxDistance, boolean spherical) { IndexAbstract matchingIndex = searchGeoIndex(true); if (matchingIndex == null) { fongoDb.notOkErrorResult(-5, "no geo indices for geoNear").throwOnError(); } LOG.info("geoNear() near:{}, query:{}, limit:{}, maxDistance:{}, spherical:{}, use index:{}", near, query, limit, maxDistance, spherical, matchingIndex.getName()); List<LatLong> latLongs = GeoUtil.latLon(Collections.<String>emptyList(), near); return ((GeoIndex) matchingIndex).geoNear(query == null ? new BasicDBObject() : query, latLongs, limit == null ? 100 : limit.intValue(), spherical); } }
public void delete(DBCollection storage, KvEntity entity) { DBObject query = new BasicDBObject(); query.put("_id", entity.id); query.put("version", entity.version); WriteResult result = storage.remove(query); result.getLastError().throwOnError(); if (result.getN()==0) throw new RuntimeException(); } }
/** * @param wc * @param wr */ protected void throwOnError(WriteConcern wc, WriteResult wr) { if (wc == null && wr.getLastConcern() == null) { CommandResult cr = wr.getLastError(); if (cr != null && cr.getErrorMessage() != null && cr.getErrorMessage().length() > 0) cr.throwOnError(); } }
protected void throwOnError(final WriteConcern wc, final WriteResult wr) { if (wc == null && wr.getLastConcern() == null) { final CommandResult cr = wr.getLastError(); if (cr != null && cr.getErrorMessage() != null && cr.getErrorMessage().length() != 0) { cr.throwOnError(); } } }
protected void throwOnError(WriteConcern wc, WriteResult wr) { if ( wc == null && wr.getLastConcern() == null) { CommandResult cr = wr.getLastError(); if (cr != null && cr.getErrorMessage() != null && cr.getErrorMessage().length() > 0) cr.throwOnError(); } } public <T> Key<T> save(String kind, T entity) {
public KvEntity update(DBCollection storage, KvEntity entity) { int version = (Integer)entity.version; DBObject query = new BasicDBObject(); query.put("_id", entity.id); query.put("version", version); DBObject record = new BasicDBObject(); record.put("version", version+1); record.put("value", entity.value); record.put("updated", entity.updated); record.put("tx", entity.tx); WriteResult result = storage.update(query, record); result.getLastError().throwOnError(); if (result.getN()==0) throw new RuntimeException(); entity.version = version+1; return entity; }
public static void update(DBCollection coll, DBObject query, DBObject data, boolean upsert, boolean multi){ // build the update command info BasicDBObject update = new BasicDBObject("q", query); update.append("u", data).append("upsert", upsert).append("multi", multi); // the command requires a list of operations BasicDBList updates = new BasicDBList(); updates.add(update); // create the command itself BasicDBObject updateCmd = new BasicDBObject("update", coll.getName()); updateCmd.append("updates", updates); // execute the command on the collections database DB configDb = coll.getDB(); CommandResult result = configDb.command(updateCmd); // convert any error to an exception and throw result.throwOnError(); }
public KvEntity create(DBCollection storage, DBObject value) { KvEntity entity = new KvEntity(); entity.version = 0; entity.value = value; entity.updated = null; entity.tx = null; DBObject record = new BasicDBObject(); record.put("version", entity.version); record.put("value", entity.value); record.put("updated", entity.updated); record.put("tx", entity.tx); storage.insert(record).getLastError().throwOnError(); entity.id = record.get("_id"); return entity; }
@Override public Role removeRole(String roleName) throws MongoException { DBCollection coll = getCollection(); Role role = getRole(roleName); if (role == null) { return null; } WriteResult result = coll.remove(getTemplateObject(role)); if (result.getLastError() != null) { result.getLastError().throwOnError(); } return role; }
@Override public void roleChanged(UserAdminEvent event) { if (UserAdminEvent.ROLE_CHANGED == event.getType()) { // Only the changes are interesting, as the creation and // removal are already caught by #addRole and #removeRole.... Role changedRole = event.getRole(); try { DBCollection coll = getCollection(); DBObject query = getTemplateObject(changedRole); DBObject update = m_helper.serializeUpdate(changedRole); WriteResult result = coll.update(query, update, false /* upsert */, false /* multi */); if (result.getLastError() != null) { result.getLastError().throwOnError(); } } catch (MongoException e) { m_log.log(LogService.LOG_WARNING, "Failed to update changed role: " + changedRole.getName(), e); } } }
@Override public void execute(PersistenceContext context) { DBObject qryObject = buildModifyQuery(); DBObject updateObject = buildDeleteBody(); MetaClass meta = getEntity().getMetaClass(); WriteResult result = MongoExecutor.update(context, meta, qryObject, updateObject); try { result.getLastError().throwOnError(); } catch (Exception e) { //we should not throw exception when delete a not existing field throw new CmsDalException(DalErrCodeEnum.ENTITY_NOT_FOUND, MessageFormat.format( "Can't found the field {0} on entity ''{1} : {2}'' for deletion!", getField().getName(), getEntity().getMetaClass().getName(), getEntity().getId())); } }
@Override public void execute(PersistenceContext context) { DBObject qryObject = buildModifyQuery(); DBObject updateObject = buildDeleteBody(); MetaClass meta = getEntity().getMetaClass(); WriteResult result = MongoExecutor.update(context, meta, qryObject, updateObject); try { result.getLastError().throwOnError(); } catch (Exception e) { //we should not throw exception when delete a not existing field throw new CmsDalException(DalErrCodeEnum.ENTITY_NOT_FOUND, MessageFormat.format( "Can't found the field {0} on entity ''{1} : {2}'' for deletion!", getField().getName(), getEntity().getMetaClass().getName(), getEntity().getId())); } }
@Override public void execute(PersistenceContext context) { DBObject qryObject = buildModifyQuery(); DBObject updateObject = buildDeleteBody(); MetaClass meta = getEntity().getMetaClass(); WriteResult result = MongoExecutor.update(context, meta, qryObject, updateObject); try { result.getLastError().throwOnError(); } catch (Exception e) { //we should not throw exception when delete a not existing field throw new CmsDalException(DalErrCodeEnum.ENTITY_NOT_FOUND, MessageFormat.format( "Can't found the field {0} on entity ''{1} : {2}'' for deletion!", getField().getName(), getEntity().getMetaClass().getName(), getEntity().getId())); } }