/** * Executes the command against the database with the given read preference. * * @param command The {@code DBObject} representation the command to be executed * @param readPreference Where to execute the command - this will only be applied for a subset of commands * @return The result of executing the command, success or failure * @mongodb.driver.manual tutorial/use-database-commands Commands * @since 2.12 */ public CommandResult command(final DBObject command, final ReadPreference readPreference) { return command(command, readPreference, null); }
/** * Executes a database command. This method constructs a simple {@link DBObject} and calls {@link DB#command(DBObject, ReadPreference) * }. * * @param command The name of the command to be executed * @param readPreference Where to execute the command - this will only be applied for a subset of commands * @return The result of the command execution * @throws MongoException if the command failed * @mongodb.driver.manual tutorial/use-database-commands Commands * @since 2.12 */ public CommandResult command(final String command, final ReadPreference readPreference) { return command(new BasicDBObject(command, true), readPreference); }
/** * Executes a database command. This method constructs a simple DBObject using {@code command} as the field name and {@code true} as its * value, and calls {@link DB#command(DBObject, ReadPreference) } with the default read preference for the database. * * @param command command to execute * @return result of command from the database * @throws MongoException if the command failed * @mongodb.driver.manual tutorial/use-database-commands Commands */ public CommandResult command(final String command) { return command(new BasicDBObject(command, Boolean.TRUE), getReadPreference()); }
/** * Executes a database command. This method calls {@link DB#command(DBObject, ReadPreference) } with the default read preference for the * database. * * @param command {@code DBObject} representation of the command to be executed * @return result of the command execution * @throws MongoException if the command failed * @mongodb.driver.manual tutorial/use-database-commands Commands */ public CommandResult command(final DBObject command) { return command(command, getReadPreference()); }
/** * Executes a database command. This method calls {@link DB#command(DBObject, ReadPreference, DBEncoder) } with the default read * preference for the database. * * @param command {@code DBObject} representation of the command to be executed * @param encoder {@link DBEncoder} to be used for command encoding * @return result of the command execution * @throws MongoException if the command failed * @mongodb.driver.manual tutorial/use-database-commands Commands */ public CommandResult command(final DBObject command, final DBEncoder encoder) { return command(command, getReadPreference(), encoder); }
/** * Forces the master server to fsync the RAM data to disk, then lock all writes. The database will be read-only after this command * returns. * * @return result of the command execution * @throws MongoException if there's a failure * @mongodb.driver.manual reference/command/fsync/ fsync command */ @Deprecated public CommandResult fsyncAndLock() { DBObject command = new BasicDBObject("fsync", 1); command.put("lock", 1); return getDB(ADMIN_DATABASE_NAME).command(command); }
/** * Forces the master server to fsync the RAM data to disk This is done automatically by the server at intervals, but can be forced for * better reliability. * * @param async if true, the fsync will be done asynchronously on the server. * @return result of the command execution * @throws MongoException if there's a failure * @mongodb.driver.manual reference/command/fsync/ fsync command */ @Deprecated public CommandResult fsync(final boolean async) { DBObject command = new BasicDBObject("fsync", 1); if (async) { command.put("async", 1); } return getDB(ADMIN_DATABASE_NAME).command(command); }
private CommandResult runIsMaster() { // Check to see if this is a replica set... if not, get out of here. return mongoClient.getDB("admin").command(new BasicDBObject("ismaster", 1)); }
@Nullable private Version getMongoVersion(DB adminDb) { final CommandResult buildInfoResult = adminDb.command("buildInfo"); if (buildInfoResult.ok()) { final BasicDBList versionArray = (BasicDBList) buildInfoResult.get("versionArray"); if (versionArray == null || versionArray.size() < 3) { LOG.debug("Couldn't retrieve MongoDB version"); return null; } final int majorVersion = (int) versionArray.get(0); final int minorVersion = (int) versionArray.get(1); final int patchVersion = (int) versionArray.get(2); return Version.forIntegers(majorVersion, minorVersion, patchVersion); } else { LOG.debug("Couldn't retrieve MongoDB buildInfo: {}", buildInfoResult.getErrorMessage()); return null; } }
db.command("{ ping: 1 }"); } catch (MongoCommandException e) { if (e.getCode() == 18) {
/** * @param version must be a major version, e.g. 1.8, 2,0, 2.2 * @return true if server is at least specified version */ protected boolean serverIsAtLeastVersion(final double version) { String serverVersion = (String) getMongoClient().getDB("admin").command("serverStatus").get("version"); return Double.parseDouble(serverVersion.substring(0, 3)) >= version; }
/** * @param version must be a major version, e.g. 1.8, 2,0, 2.2 * @return true if server is at least specified version */ protected boolean serverIsAtMostVersion(final double version) { String serverVersion = (String) getMongoClient().getDB("admin").command("serverStatus").get("version"); return Double.parseDouble(serverVersion.substring(0, 3)) <= version; }
/** * Verifies that the MD5 matches between the database and the local file. This should be called after transferring a file. * * @throws MongoException if there's a failure * @deprecated there is no replacement for this method */ @Deprecated public void validate() { if (fs == null) { throw new MongoException("no fs"); } if (md5 == null) { throw new MongoException("no md5 stored"); } DBObject cmd = new BasicDBObject("filemd5", id); cmd.put("root", fs.getBucketName()); DBObject res = fs.getDB().command(cmd); if (res != null && res.containsField("md5")) { String m = res.get("md5").toString(); if (m.equals(md5)) { return; } throw new MongoException("md5 differ. mine [" + md5 + "] theirs [" + m + "]"); } // no md5 from the server throw new MongoException("no md5 returned from server: " + res); }
private void turnOffProfilingAndDropProfileCollection() { getDb().command(new BasicDBObject("profile", 0)); DBCollection profileCollection = getDb().getCollection("system.profile"); profileCollection.drop(); }
private static BuildInfo createBuildInfo(DB adminDb) { final BuildInfo buildInfo; final CommandResult buildInfoResult = adminDb.command("buildInfo"); if (buildInfoResult.ok()) { buildInfo = BuildInfo.create( buildInfoResult.getString("version"), buildInfoResult.getString("gitVersion"), buildInfoResult.getString("sysInfo"), buildInfoResult.getString("loaderFlags"), buildInfoResult.getString("compilerFlags"), buildInfoResult.getString("allocator"), (List<Integer>) buildInfoResult.get("versionArray"), buildInfoResult.getString("javascriptEngine"), buildInfoResult.getInt("bits"), buildInfoResult.getBoolean("debug"), buildInfoResult.getLong("maxBsonObjectSize") ); } else { LOG.debug("Couldn't retrieve MongoDB buildInfo: {}", buildInfoResult.getErrorMessage()); buildInfo = null; } return buildInfo; }
@Override public void ensureCaps() { for (final MappedClass mc : mapper.getMappedClasses()) { if (mc.getEntityAnnotation() != null && mc.getEntityAnnotation().cap().value() > 0) { final CappedAt cap = mc.getEntityAnnotation().cap(); final String collName = mapper.getCollectionName(mc.getClazz()); final BasicDBObjectBuilder dbCapOpts = start("capped", true); if (cap.value() > 0) { dbCapOpts.add("size", cap.value()); } if (cap.count() > 0) { dbCapOpts.add("max", cap.count()); } final DB database = getDB(); if (database.getCollectionNames().contains(collName)) { final DBObject dbResult = database.command(start("collstats", collName).get()); if (((CommandResult) dbResult).getBoolean("capped")) { LOG.debug("DBCollection already exists and is capped already; doing nothing. " + dbResult); } else { LOG.warn("DBCollection already exists with same name(" + collName + ") and is not capped; not creating capped version!"); } } else { getDB().createCollection(collName, dbCapOpts.get()); LOG.debug("Created capped DBCollection (" + collName + ") with opts " + dbCapOpts); } } } }
private static HostInfo createHostInfo(DB adminDb) { final HostInfo hostInfo; final CommandResult hostInfoResult = adminDb.command("hostInfo"); if (hostInfoResult.ok()) { final BasicDBObject systemMap = (BasicDBObject) hostInfoResult.get("system");
@Test @SuppressWarnings("deprecation") public void testCommentsShowUpInLogsOld() { getDs().save(asList(new Pic("pic1"), new Pic("pic2"), new Pic("pic3"), new Pic("pic4"))); getDb().command(new BasicDBObject("profile", 2)); String expectedComment = "test comment"; toList(getDs().find(Pic.class).comment(expectedComment).find()); DBCollection profileCollection = getDb().getCollection("system.profile"); assertNotEquals(0, profileCollection.count()); DBObject profileRecord = profileCollection.findOne(new BasicDBObject("op", "query") .append("ns", getDs().getCollection(Pic.class).getFullName())); assertEquals(profileRecord.toString(), expectedComment, getCommentFromProfileRecord(profileRecord)); turnOffProfilingAndDropProfileCollection(); }
@Test public void testCommentsShowUpInLogs() { getDs().save(asList(new Pic("pic1"), new Pic("pic2"), new Pic("pic3"), new Pic("pic4"))); getDb().command(new BasicDBObject("profile", 2)); String expectedComment = "test comment"; toList(getDs().find(Pic.class) .find(new FindOptions() .modifier("$comment", expectedComment))); DBCollection profileCollection = getDb().getCollection("system.profile"); assertNotEquals(0, profileCollection.count()); DBObject profileRecord = profileCollection.findOne(new BasicDBObject("op", "query") .append("ns", getDs().getCollection(Pic.class).getFullName())); assertEquals(profileRecord.toString(), expectedComment, getCommentFromProfileRecord(profileRecord)); turnOffProfilingAndDropProfileCollection(); }
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(); } } } }