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; }
/** * Gets the "ok" field, which is whether this command executed correctly or not. * * @return true if the command executed without error. */ public boolean ok() { Object okValue = get("ok"); if (okValue instanceof Boolean) { return (Boolean) okValue; } else if (okValue instanceof Number) { return ((Number) okValue).intValue() == 1; } else { return false; } }
@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; } }
final CommandResult dbStatsResult = db.command("dbStats"); if (dbStatsResult.ok()) { final BasicDBObject extentFreeListMap = (BasicDBObject) dbStatsResult.get("extentFreeList"); final DatabaseStats.ExtentFreeList extentFreeList; if (extentFreeListMap == null) { final BasicDBObject dataFileVersionMap = (BasicDBObject) dbStatsResult.get("dataFileVersion"); final DatabaseStats.DataFileVersion dataFileVersion; if (dataFileVersionMap == null) { final CommandResult serverStatusResult = adminDb.command("serverStatus"); if (serverStatusResult.ok()) { final BasicDBObject connectionsMap = (BasicDBObject) serverStatusResult.get("connections"); final ServerStatus.Connections connections = ServerStatus.Connections.create( connectionsMap.getInt("current"), final BasicDBObject networkMap = (BasicDBObject) serverStatusResult.get("network"); final ServerStatus.Network network = ServerStatus.Network.create( networkMap.getInt("bytesIn"), final BasicDBObject memoryMap = (BasicDBObject) serverStatusResult.get("mem"); final ServerStatus.Memory memory = ServerStatus.Memory.create( memoryMap.getInt("bits"), final BasicDBObject storageEngineMap = (BasicDBObject) serverStatusResult.get("storageEngine"); final ServerStatus.StorageEngine storageEngine; if (storageEngineMap == null) {
/** * Checks whether this collection is capped * * @return true if this is a capped collection * @mongodb.driver.manual /core/capped-collections/#check-if-a-collection-is-capped Capped Collections */ public boolean isCapped() { CommandResult commandResult = getStats(); Object cappedField = commandResult.get("capped"); return cappedField != null && (cappedField.equals(1) || cappedField.equals(true)); }
final CommandResult hostInfoResult = adminDb.command("hostInfo"); if (hostInfoResult.ok()) { final BasicDBObject systemMap = (BasicDBObject) hostInfoResult.get("system"); final HostInfo.System system = HostInfo.System.create( new DateTime(systemMap.getDate("currentTime")), systemMap.getBoolean("numaEnabled") ); final BasicDBObject osMap = (BasicDBObject) hostInfoResult.get("os"); final HostInfo.Os os = HostInfo.Os.create( osMap.getString("type"), ); final BasicDBObject extraMap = (BasicDBObject) hostInfoResult.get("extra"); final HostInfo.Extra extra = HostInfo.Extra.create( extraMap.getString("versionString"),
/** * Gets the error message associated with a failed command. * * @return The error message or null */ @Nullable public String getErrorMessage() { Object foo = get("errmsg"); if (foo == null) { return null; } return foo.toString(); }
logger.trace("Command executed return : {}", cr); logger.info("MongoDB version - {}", cr.get("version")); if (logger.isTraceEnabled()) { logger.trace("serverStatus: {}", cr); if (cr.get("process") == null) { logger.warn("serverStatus.process return null."); return false; String process = cr.get("process").toString().toLowerCase(); if (logger.isTraceEnabled()) { logger.trace("process: {}", process);
/** * 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 boolean isReplicaSet() { return runIsMaster().get("setName") != null; }
/** * @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; }
/** * @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; }
CommandResult stats = collection.getStats(); Object maxObj = stats.get(MongoDBConstants.MAX); Assert.assertNotNull(maxObj); int max = Integer.parseInt(maxObj.toString());
DBObject unwindParam = new BasicDBObject("$unwind","$dataItems"); DBObject matchParam = new BasicDBObject("$match", new BasicDBObject("dataItems.version", new BasicDBObject("$gt",requestedModule.getVersion()))); DBObject fields = new BasicDBObject("dataItems", 1); DBObject projectParam = new BasicDBObject("$project", fields); AggregationOutput output = mongoTemplate.getCollection( "appModules").aggregate( unwindParam, matchParam,projectParam); CommandResult updatedData = output.getCommandResult(); BasicDBList resList = (BasicDBList) updatedData.get("result");
/** * Checks whether this collection is capped * * @return true if this is a capped collection * @mongodb.driver.manual /core/capped-collections/#check-if-a-collection-is-capped Capped Collections */ public boolean isCapped() { CommandResult commandResult = getStats(); Object cappedField = commandResult.get("capped"); return cappedField != null && (cappedField.equals(1) || cappedField.equals(true)); }
CommandResult commandResult = db.command(searchCmd); BasicDBList results = (BasicDBList)commandResult.get("results"); for( Iterator< Object > it = results.iterator(); it.hasNext(); ) { BasicDBObject result = (BasicDBObject) it.next(); BasicDBObject dbo = (BasicDBObject) result.get("obj"); System.out.println(dbo.getString("Filename")); }
/** * @return number of affected documents */ protected int getN() { return wr.getLastError().containsField("n") ? ((Number) wr.getLastError().get("n")).intValue() : 0; }
/** * @return the new _id field if an insert/upsert was performed */ public Object getNewId() { return getInsertedCount() == 1 && wr.getLastError().containsField("upserted") ? wr.getLastError().get("upserted") : null; }
/** * @return the new _id field if an insert/upsert was performed */ public Object getNewId() { return getInsertedCount() == 1 && wr.getLastError().containsField("upserted") ? wr.getLastError().get("upserted") : null; }