Example usage for com.mongodb Mongo getDB

List of usage examples for com.mongodb Mongo getDB

Introduction

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

Prototype

@Deprecated 
public DB getDB(final String dbName) 

Source Link

Document

Gets a database object.

Usage

From source file:org.greenmongoquery.db.service.impl.MongoServiceImpl.java

License:Open Source License

public String updateDocument(String json, String dbname, String collectionName, Mongo mongo, String id) {
    logger.info("start update");
    String result = null;/*from  www  .j  av a 2s. com*/
    DB db = mongo.getDB(dbname);
    DBObject orgDbObject = new BasicDBObject("_id", new ObjectId(id));

    // id ="509e55b844aefa5410c8e656";
    // orgDbObject.put("_id", id);
    logger.info("update id " + id);
    logger.info("Object to update" + orgDbObject.toString());

    DBCollection coll = db.getCollection(collectionName);
    DBObject found = coll.findOne(orgDbObject);
    DBObject dbObject = (DBObject) JSON.parse(json);
    coll.update(found, dbObject);

    result = "update document : _id=  " + id;
    return result;
}

From source file:org.greenmongoquery.db.service.impl.MongoServiceImpl.java

License:Open Source License

public String deleteDocument(String json, String dbname, String collectionName, Mongo mongo, String id) {

    String result = null;//  w  w  w .  ja  v  a2s.c  o m
    DB db = mongo.getDB(dbname);
    DBObject orgDbObject = new BasicDBObject("_id", new ObjectId(id));
    logger.info("id " + id);
    logger.info("k object " + orgDbObject.toString());

    DBCollection coll = db.getCollection(collectionName);
    DBObject found = coll.findOne(orgDbObject);
    coll.remove(found);
    result = "delete document : _id=  " + id;
    return result;

}

From source file:org.greenmongoquery.db.service.impl.MongoServiceImpl.java

License:Open Source License

@Override
public void insertCollection(String db, String collection, Mongo mongo) {
    DBObject option = new BasicDBObject();
    DB dbs = mongo.getDB(db);
    dbs.createCollection(collection, option);

}

From source file:org.greenmongoquery.db.service.impl.MongoServiceImpl.java

License:Open Source License

@Override
public void insertDb(String db, Mongo mongo) {
    DBObject option = new BasicDBObject();

    DB dbs = mongo.getDB(db);
    dbs.createCollection("Dummmy_Collection", option);

    List<String> dbStr = getDbs(mongo);
    logger.info("Database : s" + dbStr);
    dbs.getCollection("Dummmy_Collection").drop();
    dbStr = getDbs(mongo);/*from  w w w .j a  v  a 2 s. c o m*/
    // System.out.println(dbStr);
}

From source file:org.greenmongoquery.db.service.impl.MongoServiceImpl.java

License:Open Source License

@Override
public void updatetDb(String db, String updateDb, Mongo mongo) {
    DB dbs = mongo.getDB("admin");
    DBObject cmd = new BasicDBObject();
    DBObject key = new BasicDBObject();
    logger.info("host " + mongo.getAddress().getHost() + " from db " + db + " updte db " + updateDb);

    cmd.put("copydb", 1);

    cmd.put("fromhost", mongo.getAddress().getHost());
    cmd.put("todb", updateDb);
    cmd.put("fromdb", db);

    CommandResult result = dbs.command(cmd);

    DB dbsDop = mongo.getDB(db);/*from  www  .  j  a  v a  2s .  com*/
    dbsDop.dropDatabase();
    logger.info(result.getErrorMessage() + result.getException());
}

From source file:org.greenmongoquery.db.service.impl.MongoServiceImpl.java

License:Open Source License

@Override
public void deleteDb(String db, Mongo mongo) {
    DB dbsDop = mongo.getDB(db);
    dbsDop.dropDatabase();
}

From source file:org.greenmongoquery.db.service.impl.MongoServiceImpl.java

License:Open Source License

@Override
public void updateCollection(String db, String collection, String updatecollection, Mongo mongo) {
    DBObject option = new BasicDBObject();
    DB dbs = mongo.getDB(db);
    DBCollection coll = dbs.getCollection(collection);
    coll.rename(updatecollection, true);

}

From source file:org.greenmongoquery.db.service.impl.MongoServiceImpl.java

License:Open Source License

@Override
public void deleteCollection(String db, String collection, Mongo mongo) {
    DB dbs = mongo.getDB(db);
    DBCollection coll = dbs.getCollection(collection);
    coll.drop();//from ww w . j  a va2  s.c o  m
}

From source file:org.greenmongoquery.db.service.impl.MongoServiceImpl.java

License:Open Source License

@Override
public MongoCollectionModel getDocumentsByQuery(Mongo mongo, String db, String collection, String query) {
    Map<String, String> queryDelimit = org.greenmongoquery.util.Util.getQuerySeperate(query);
    collection = (String) queryDelimit.get("coll");
    query = queryDelimit.get("query");
    logger.info("Query " + query);

    Gson gson = new GsonBuilder().create();
    MongoCollectionModel model = new MongoCollectionModel();
    StringBuilder bsonData = new StringBuilder();
    List<Map> result = new ArrayList<Map>();
    DBObject object = (DBObject) JSON.parse(query);
    DB dbs = mongo.getDB(db);
    DBCollection coll = dbs.getCollection(collection);
    DBCursor cursor = coll.find(object);
    Map data = new TreeMap();
    String output = null;//from  w w w. ja va  2 s . com
    try {
        while (cursor.hasNext()) {
            output = cursor.next().toString();
            bsonData.append(output + "\n");
            data = gson.fromJson(output, Map.class);

            result.add(data);

        }
    } finally {
        cursor.close();
    }
    model.setBsonData(bsonData);
    model.setResult(result);
    return model;

}

From source file:org.helios.dashkuj.core.Dashkuj.java

License:Open Source License

/**
 * Synchronizes the api keys from mongoDB to redis for cases when redis loses them
 * @param redisHost The redis host//from   w  w w .j a  v a2 s  . co m
 * @param redisPort The redis port
 * @param mongoHost The mongodb host
 * @param mongoPort The mongodb port
 * FIXME:  Need to support credentials
 */
public void synchRedisApiKeys(String redisHost, int redisPort, String mongoHost, int mongoPort) {
    Mongo mongo = null;
    DBCursor cursor = null;
    Jedis jedis = null;
    try {
        mongo = new Mongo(mongoHost, mongoPort);
        jedis = new Jedis(redisHost, redisPort);
        log.info("Synchronizing APIKeys between mongo [{}:{}] and redis [{}:{}]", mongoHost, mongoPort,
                redisHost, redisPort);
        Map<String, String> apiKeys = jedis.hgetAll("apiKeys");
        DB db = mongo.getDB("dashku_development");
        DBCollection dbColl = db.getCollection("users");
        cursor = dbColl.find();
        while (cursor.hasNext()) {
            DBObject dbo = cursor.next();
            String apiKey = dbo.get("apiKey").toString();
            String id = dbo.get("_id").toString();
            String user = dbo.get("username").toString();
            log.debug("Inspecting user [" + user + "]");
            if (!apiKeys.containsKey(apiKey)) {
                jedis.hmset("apiKeys", Collections.singletonMap(apiKey, id));
                log.info("Added missing redis entry [" + apiKey + "] for user [" + user + "]");
            }
        }
        log.info("Redis Synch Complete");
    } catch (Exception ex) {
        log.error("Redis Synch Failed", ex);
        throw new RuntimeException("Redis Synch Failed", ex);
    } finally {
        if (cursor != null)
            try {
                cursor.close();
            } catch (Exception e) {
            }
        if (mongo != null)
            try {
                mongo.close();
            } catch (Exception e) {
            }
        if (jedis != null)
            try {
                jedis.disconnect();
            } catch (Exception e) {
            }
    }
}