Example usage for com.mongodb CommandResult toString

List of usage examples for com.mongodb CommandResult toString

Introduction

In this page you can find the example usage for com.mongodb CommandResult toString.

Prototype

@SuppressWarnings("deprecation")
public String toString() 

Source Link

Document

Returns a JSON serialization of this object

The output will look like: {"a":1, "b":["x","y","z"]} }

Usage

From source file:com.hangum.tadpole.mongodb.core.test.MongoTestReIndex.java

License:Open Source License

/**
 * @param args// w w  w .  ja  v a 2 s.  c  om
 */
public static void main(String[] args) throws Exception {
    ConAndAuthentication testMongoCls = new ConAndAuthentication();
    Mongo mongo = testMongoCls.connection(ConAndAuthentication.serverurl, ConAndAuthentication.port);
    DB db = mongo.getDB("test");

    DBObject queryObj = new BasicDBObject("reIndex", "store");
    CommandResult cr = db.command(queryObj);

    System.out.println("[ok]" + cr.ok());
    if (!cr.ok())
        System.out.println("[Exception ]" + cr.getException().toString());
    System.out.println("[toString]" + cr.toString());
    System.out.println("[size]" + cr.size());

    mongo.close();
}

From source file:com.hangum.tadpole.mongodb.core.test.MongoTestServerStatus.java

License:Open Source License

/**
 * @param args/* w  w  w.  java2 s. c o m*/
 */
public static void main(String[] args) throws Exception {
    ConAndAuthentication testMongoCls = new ConAndAuthentication();
    Mongo mongo = testMongoCls.connection(ConAndAuthentication.serverurl, ConAndAuthentication.port);
    DB db = mongo.getDB("test");

    DBObject queryObj = new BasicDBObject("serverStatus", 0);
    CommandResult cr = db.command(queryObj);

    String strHost = cr.getString("host");
    String version = cr.getString("version");
    String process = cr.getString("process");
    String pid = cr.getString("pid");
    String uptime = cr.getString("uptime");
    String uptimeMillis = cr.getString("uptimeMillis");
    String uptimeEstimate = cr.getString("uptimeEstimate");
    String localTime = cr.getString("localTime");

    System.out.println("[strHost]\t " + strHost);
    System.out.println("[version]\t " + version);

    System.out.println("[process]\t " + process);
    System.out.println("[pid]\t " + pid);
    System.out.println("[uptime]\t " + uptime);
    System.out.println("[uptimeMillis]\t " + uptimeMillis);
    System.out.println("[uptimeEstimate]\t " + uptimeEstimate);
    System.out.println("[localTime]\t " + localTime);

    System.out.println("[ok]" + cr.ok());
    if (!cr.ok())
        System.out.println("[Exception ]" + cr.getException().toString());
    System.out.println("[toString]" + cr.toString());
    System.out.println("[size]" + cr.size());

    mongo.close();
}

From source file:com.hangum.tadpole.mongodb.core.test.MongoTestShardInformation.java

License:Open Source License

/**
 * @param args/*www . ja va 2  s  .  c o  m*/
 */
public static void main(String[] args) throws Exception {
    ConAndAuthentication testMongoCls = new ConAndAuthentication();
    Mongo mongo = testMongoCls.connection("127.0.0.1", 27018);
    DB db = mongo.getDB("admin");

    DBObject queryObj = new BasicDBObject("listshards", 1);
    CommandResult cr = db.command(queryObj);
    if (cr.ok()) {
        System.out.println(cr.toString());
    } else {
        System.out.println(cr.getException());
    }

    // shard key ?? ? ?  .
    final BasicDBObject shardKey = new BasicDBObject("TrackId", 1);
    final BasicDBObject cmd = new BasicDBObject("shardcollection", "test.Track");
    cmd.put("key", shardKey);
    CommandResult result4 = mongo.getDB("admin").command(cmd);

    System.out.println("====>" + result4);
}

From source file:com.hangum.tadpole.mongodb.core.test.MongoTestTop.java

License:Open Source License

/**
 * @param args// w  w  w  .j a  v  a  2s .  co m
 */
public static void main(String[] args) throws Exception {
    ConAndAuthentication testMongoCls = new ConAndAuthentication();
    Mongo mongo = testMongoCls.connection(ConAndAuthentication.serverurl, ConAndAuthentication.port);
    DB db = mongo.getDB("admin");

    DBObject queryObj = new BasicDBObject("top", 1);
    CommandResult cr = db.command(queryObj);
    if (cr.ok()) {
        System.out.println(cr.toString());
    } else {

        System.out.println(cr.getException());
    }
}

From source file:com.sangupta.jerry.mongodb.MongoDBUtils.java

License:Apache License

/**
 * Returns the MongoDB statistics for the given database.
 * /*from ww w.j a  v a  2s . c om*/
 * @param mongoDatabase
 * 
 * @return
 * 
 * @throws NullPointerException if database instance provided is <code>null</code>.
 */
public static MongoDBStats getDatabaseStatistics(DB mongoDatabase) {
    CommandResult commandResult = mongoDatabase.getStats();
    MongoDBStats stats = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.IDENTITY).create()
            .fromJson(commandResult.toString(), MongoDBStats.class);
    return stats;
}

From source file:de.flapdoodle.embed.mongo.tests.MongosSystemForTestFactory.java

License:Apache License

private void configureMongos() throws Exception {
    CommandResult cr;
    MongoOptions mo = new MongoOptions();
    mo.autoConnectRetry = true;//from  w  w w  .  jav a 2s . c om
    Mongo mongo = new Mongo(
            new ServerAddress(this.config.net().getServerAddress().getHostName(), this.config.net().getPort()),
            mo);
    DB mongoAdminDB = mongo.getDB(ADMIN_DATABASE_NAME);

    // Add shard from the replica set list
    for (Entry<String, List<IMongodConfig>> entry : this.replicaSets.entrySet()) {
        String replicaName = entry.getKey();
        String command = "";
        for (IMongodConfig mongodConfig : entry.getValue()) {
            if (command.isEmpty()) {
                command = replicaName + "/";
            } else {
                command += ",";
            }
            command += mongodConfig.net().getServerAddress().getHostName() + ":" + mongodConfig.net().getPort();
        }
        logger.info("Execute add shard command: " + command);
        cr = mongoAdminDB.command(new BasicDBObject("addShard", command));
        logger.info(cr.toString());
    }

    logger.info("Execute list shards.");
    cr = mongoAdminDB.command(new BasicDBObject("listShards", 1));
    logger.info(cr.toString());

    // Enabled sharding at database level
    logger.info("Enabled sharding at database level");
    cr = mongoAdminDB.command(new BasicDBObject("enableSharding", this.shardDatabase));
    logger.info(cr.toString());

    // Create index in sharded collection
    logger.info("Create index in sharded collection");
    DB db = mongo.getDB(this.shardDatabase);
    db.getCollection(this.shardCollection).ensureIndex(this.shardKey);

    // Shard the collection
    logger.info("Shard the collection: " + this.shardDatabase + "." + this.shardCollection);
    DBObject cmd = new BasicDBObject();
    cmd.put("shardCollection", this.shardDatabase + "." + this.shardCollection);
    cmd.put("key", new BasicDBObject(this.shardKey, 1));
    cr = mongoAdminDB.command(cmd);
    logger.info(cr.toString());

    logger.info("Get info from config/shards");
    DBCursor cursor = mongo.getDB("config").getCollection("shards").find();
    while (cursor.hasNext()) {
        DBObject item = cursor.next();
        logger.info(item.toString());
    }

}

From source file:dk.au.cs.karibu.backend.mongo.MongoDBStorage.java

License:Apache License

/**
 * {@inheritDoc}/* ww  w  .  j a  va 2 s. c  om*/
 */
@Override
public void process(String producerCode, BasicDBObject dbo) {
    // get a collection object to work with 
    coll = database.getCollection(producerCode);
    // Insert it into Mongo 
    WriteResult writeResult = coll.insert(dbo);
    CommandResult err = writeResult.getCachedLastError();
    if (!err.ok()) {
        log.error("MongoDB insert failed with result: " + err.toString());
    }
}

From source file:dk.au.cs.karibu.backend.mongo.MongoDBStorage.java

License:Apache License

@Override
public void store(BasicDBObject dbo) {
    // get the karibu statistics collection object
    coll = database.getCollection(KARIBU_STATISTIC_COLLECTION_NAME);
    // Insert it into Mongo 
    WriteResult writeResult = coll.insert(dbo);
    CommandResult err = writeResult.getCachedLastError();
    if (!err.ok()) {
        log.error("MongoDB insert failed with result: " + err.toString());
    }/*ww  w  .  j  ava2  s .c o  m*/
    // System.err.println("Will store "+dbo);
}

From source file:example.springdata.mongodb.util.MongosSystemForTestFactory.java

License:Apache License

private void configureMongos() throws Exception {
    CommandResult cr;
    MongoClientOptions options = MongoClientOptions.builder().connectTimeout(10).build();
    try (MongoClient mongo = new MongoClient(
            new ServerAddress(this.config.net().getServerAddress().getHostName(), this.config.net().getPort()),
            options)) {//from ww  w .j a v  a2 s  . co m
        DB mongoAdminDB = mongo.getDB(ADMIN_DATABASE_NAME);

        // Add shard from the replica set list
        for (Entry<String, List<IMongodConfig>> entry : this.replicaSets.entrySet()) {
            String replicaName = entry.getKey();
            String command = "";
            for (IMongodConfig mongodConfig : entry.getValue()) {
                if (command.isEmpty()) {
                    command = replicaName + "/";
                } else {
                    command += ",";
                }
                command += mongodConfig.net().getServerAddress().getHostName() + ":"
                        + mongodConfig.net().getPort();
            }
            logger.info("Execute add shard command: {}", command);
            cr = mongoAdminDB.command(new BasicDBObject("addShard", command));
            logger.info(cr.toString());
        }

        logger.info("Execute list shards.");
        cr = mongoAdminDB.command(new BasicDBObject("listShards", 1));
        logger.info(cr.toString());

        // Enabled sharding at database level
        logger.info("Enabled sharding at database level");
        cr = mongoAdminDB.command(new BasicDBObject("enableSharding", this.shardDatabase));
        logger.info(cr.toString());

        // Create index in sharded collection
        logger.info("Create index in sharded collection");
        DB db = mongo.getDB(this.shardDatabase);
        db.getCollection(this.shardCollection).createIndex(this.shardKey);

        // Shard the collection
        logger.info("Shard the collection: {}.{}", this.shardDatabase, this.shardCollection);
        DBObject cmd = new BasicDBObject();
        cmd.put("shardCollection", this.shardDatabase + "." + this.shardCollection);
        cmd.put("key", new BasicDBObject(this.shardKey, 1));
        cr = mongoAdminDB.command(cmd);
        logger.info(cr.toString());

        logger.info("Get info from config/shards");
        DBCursor cursor = mongo.getDB("config").getCollection("shards").find();
        while (cursor.hasNext()) {
            DBObject item = cursor.next();
            logger.info(item.toString());
        }
    }

}

From source file:net.cit.tetrad.rrd.dao.MongoStatusToMonitorImpl.java

License:Open Source License

public String getAllServerStatus(Mongo mongo, Device device, String command, String databaseName)
        throws MongoException, Exception {
    String objtoString;/*  w w  w .  ja v a  2 s . c o  m*/
    try {
        DB db = mongo.getDB(databaseName);
        CommandResult commandResult = db.command(command);
        objtoString = commandResult.toString();
    } catch (DataAccessResourceFailureException e) {
        e.printStackTrace();
        throw new MongoException(e.getMessage());
    } catch (MongoException e) {
        // alarm  ?
        e.printStackTrace();
        throw e;
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    } finally {

    }
    return objtoString;
}