Example usage for com.mongodb BasicDBObject BasicDBObject

List of usage examples for com.mongodb BasicDBObject BasicDBObject

Introduction

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

Prototype

public BasicDBObject(final String key, final Object value) 

Source Link

Document

Creates an object with the given key/value

Usage

From source file:br.com.ifspsaocarlos.gastock.models.MTanque.java

@Override
public Tanque get(int tanqueId) throws Exception {

    DBObject dados = this.banco.buscaRegistro(new BasicDBObject("cod", tanqueId));

    Tanque result = new Tanque((int) dados.get("cod"), dados.get("combustivel").toString(),
            Double.parseDouble(dados.get("quantidade").toString()),
            Double.parseDouble(dados.get("preco").toString()));

    return result;
}

From source file:brooklyn.entity.nosql.mongodb.MongoDBClientSupport.java

License:Apache License

public Optional<CommandResult> runDBCommand(String database, String command) {
    return runDBCommand(database, new BasicDBObject(command, Boolean.TRUE));
}

From source file:brooklyn.entity.nosql.mongodb.MongoDBClientSupport.java

License:Apache License

public boolean ping() {
    DBObject ping = new BasicDBObject("ping", "1");
    try {/*from  w  ww.  j  a v a  2  s . c  om*/
        runDBCommand("admin", ping);
    } catch (MongoException e) {
        return false;
    }
    return true;
}

From source file:brooklyn.entity.nosql.mongodb.MongoDBClientSupport.java

License:Apache License

public boolean initializeReplicaSet(String replicaSetName, Integer id) {
    HostAndPort primary = getServerHostAndPort();
    BasicBSONObject config = ReplicaSetConfig.builder(replicaSetName).member(primary, id).build();

    BasicDBObject dbObject = new BasicDBObject("replSetInitiate", config);
    LOG.debug("Initiating replica set with: " + dbObject);

    Optional<CommandResult> result = runDBCommand("admin", dbObject);
    if (result.isPresent() && result.get().ok() && LOG.isDebugEnabled()) {
        LOG.debug("Completed initiating MongoDB replica set {} on entity {}", replicaSetName, this);
    }//w  w w.j a v  a 2s  .  com
    return result.isPresent() && result.get().ok();
}

From source file:brooklyn.entity.nosql.mongodb.MongoDBClientSupport.java

License:Apache License

/**
 * Runs replSetReconfig with the given BasicBSONObject. Returns true if the result's
 * status is ok./*w w w  . ja  va  2s  .c  om*/
 */
private boolean reconfigureReplicaSet(BasicBSONObject newConfig) {
    BasicDBObject command = new BasicDBObject("replSetReconfig", newConfig);
    LOG.debug("Reconfiguring replica set to: " + command);
    Optional<CommandResult> result = runDBCommand("admin", command);
    return result.isPresent() && result.get().ok();
}

From source file:brooklyn.entity.nosql.mongodb.MongoDBClientSupport.java

License:Apache License

public boolean addShardToRouter(String hostAndPort) {
    LOG.debug("Adding shard " + hostAndPort);
    BasicDBObject command = new BasicDBObject("addShard", hostAndPort);
    Optional<CommandResult> result = runDBCommand("admin", command);
    return result.isPresent() && result.get().ok();
}

From source file:brooklyn.entity.nosql.mongodb.MongoDBTestHelper.java

License:Apache License

/**
 * Inserts a new object with { key: value } at given server.
 * @return The new document's id/* w  w  w  .  j a va  2 s  . c  om*/
 */
public static String insert(AbstractMongoDBServer entity, String key, Object value) {
    LOG.info("Inserting {}:{} at {}", new Object[] { key, value, entity });
    MongoClient mongoClient = clientForServer(entity);
    try {
        DB db = mongoClient.getDB(TEST_DB);
        DBCollection testCollection = db.getCollection(TEST_COLLECTION);
        BasicDBObject doc = new BasicDBObject(key, value);
        testCollection.insert(doc);
        ObjectId id = (ObjectId) doc.get("_id");
        return id.toString();
    } finally {
        mongoClient.close();
    }
}

From source file:brooklyn.entity.nosql.mongodb.MongoDBTestHelper.java

License:Apache License

/** @return The {@link DBObject} representing the object with the given id */
public static DBObject getById(AbstractMongoDBServer entity, String id) {
    LOG.info("Getting {} from {}", new Object[] { id, entity });
    MongoClient mongoClient = clientForServer(entity);
    // Secondary preferred means the driver will let us read from secondaries too.
    mongoClient.setReadPreference(ReadPreference.secondaryPreferred());
    try {//from w  w w  . j a v a2  s.com
        DB db = mongoClient.getDB(TEST_DB);
        DBCollection testCollection = db.getCollection(TEST_COLLECTION);
        return testCollection.findOne(new BasicDBObject("_id", new ObjectId(id)));
    } finally {
        mongoClient.close();
    }
}

From source file:BusinessLogic.Service.RestaurantService.java

public String addRestaurant(Restaurant rs) {
    //String name, String direccion, String phone) 
    String name = rs.getName();/*from w w  w  .  j  a  v  a2 s .c  om*/
    String direccion = rs.getAddress();
    String phone = rs.getPhone();
    String restaurantCreate = null;

    try {
        // To connect to mongo dbserver
        MongoConnection dbSingleton = MongoConnection.getInstance();
        DB db = dbSingleton.getTestdb();
        DBCollection coll = db.getCollection(collName);

        System.out.println("Collection restaurants selected successfully");

        BasicDBObject doc = new BasicDBObject("name", name).append("direccion", direccion).append("phone",
                phone);
        coll.insert(doc);
        System.out.println("Document inserted successfully");
        restaurantCreate = "Success";

    } catch (Exception e) {
        System.err.println(e.getClass().getName() + ": " + e.getMessage());
    }

    if (restaurantCreate != null) {
        return "The restaurant has been created successfully!";
    } else {
        return "The restaurant has not been created!";
    }

}

From source file:BusinessLogic.Service.RestaurantService.java

public String addRestaurant(String name, String direccion, String phone) {
    String restaurantCreate = null;

    try {/*  ww w. j  a va 2 s  .c  o  m*/
        // To connect to mongo dbserver
        MongoConnection dbSingleton = MongoConnection.getInstance();
        DB db = dbSingleton.getTestdb();
        DBCollection coll = db.getCollection(collName);

        System.out.println("Collection restaurants selected successfully");

        BasicDBObject doc = new BasicDBObject("name", name).append("direccion", direccion).append("phone",
                phone);
        coll.insert(doc);
        System.out.println("Document inserted successfully");
        restaurantCreate = "Success";

    } catch (Exception e) {
        System.err.println(e.getClass().getName() + ": " + e.getMessage());
    }

    if (restaurantCreate != null) {
        return "The restaurant has been created successfully!";
    } else {
        return "The restaurant has not been created!";
    }
}