Example usage for com.mongodb MongoClient getDB

List of usage examples for com.mongodb MongoClient getDB

Introduction

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

Prototype

@Deprecated 
public DB getDB(final String dbName) 

Source Link

Document

Gets a database object.

Usage

From source file:org.aw20.mongoworkbench.command.CreateDbsMongoCommand.java

License:Open Source License

@Override
public void execute() throws Exception {
    MongoClient mdb = MongoFactory.getInst().getMongo(sName);

    if (mdb == null)
        throw new Exception("no server selected");

    DB db = mdb.getDB(sDb);

    String colTemp = "tmp" + System.currentTimeMillis();
    DBCollection coll = db.createCollection(colTemp, new BasicDBObject());
    coll.drop();//w  w w.j a  v a  2  s  . co m

    setDBNames(mdb);
}

From source file:org.aw20.mongoworkbench.command.DBserverStatsMongoCommand.java

License:Open Source License

@Override
public void execute() throws Exception {
    MongoClient mdb = MongoFactory.getInst().getMongo(sName);

    if (mdb == null)
        throw new Exception("no server selected");

    String db;/*from w ww.  ja  va 2  s .  c  om*/
    List<String> listDBs = mdb.getDatabaseNames();
    if (listDBs.size() > 0)
        db = listDBs.get(0);
    else
        db = "admin";

    DB dbM = mdb.getDB(db);

    Object obj = dbM.eval("db.serverStatus()", (Object[]) null);
    if (obj instanceof Map) {
        map = (Map) obj;
        setMessage("Server Status - version=" + map.get("version") + "; localTime=" + map.get("localTime"));
    } else {
        map = null;
        setMessage("invalid result returned");
    }
}

From source file:org.aw20.mongoworkbench.command.DBStatsMongoCommand.java

License:Open Source License

@Override
public void execute() throws Exception {
    MongoClient mdb = MongoFactory.getInst().getMongo(sName);

    if (mdb == null)
        throw new Exception("no server selected");

    setDBNames(mdb);/*w w w.ja v  a2s.c  om*/
    Iterator<String> it = dbNames.iterator();
    while (it.hasNext()) {
        String db = it.next();

        CommandResult cmdr = mdb.getDB(db).getStats();
        statsListMap.add(transform(cmdr.toMap()));
    }

    setMessage("Retrived Database Stats; db=" + statsListMap.size());
}

From source file:org.aw20.mongoworkbench.command.DropCollectionMongoCommand.java

License:Open Source License

@Override
public void execute() throws Exception {
    MongoClient mdb = MongoFactory.getInst().getMongo(sName);
    if (mdb == null)
        throw new Exception("no server selected");

    if (sDb == null)
        throw new Exception("no database selected");

    MongoFactory.getInst().setActiveDB(sDb);

    DB db = mdb.getDB(sDb);

    DBCollection collection = db.getCollection(sColl);
    collection.drop();/*from w w w.ja  v  a2  s .co m*/

    super.execute();
}

From source file:org.aw20.mongoworkbench.command.DropDbsMongoCommand.java

License:Open Source License

@Override
public void execute() throws Exception {
    MongoClient mdb = MongoFactory.getInst().getMongo(sName);

    if (mdb == null)
        throw new Exception("no server selected");

    DB db = mdb.getDB(sDb);
    db.dropDatabase();/*from  w ww  .j  a  v  a2  s.c o  m*/

    setDBNames(mdb);

    MongoFactory.getInst().setActiveDB(null);
}

From source file:org.aw20.mongoworkbench.command.EvalMongoCommand.java

License:Open Source License

@Override
public void execute() throws Exception {
    MongoClient mdb = MongoFactory.getInst().getMongo(sName);

    if (mdb == null)
        throw new Exception("no server selected");

    String db;/*from w  w w .  ja va 2s  .  c o  m*/
    List<String> listDBs = mdb.getDatabaseNames();
    if (listDBs.size() > 0)
        db = listDBs.get(0);
    else
        db = "admin";

    DB dbM = mdb.getDB(db);

    Object obj = dbM.eval(eval, (Object[]) null);
    if (obj instanceof Map) {
        map = (Map) obj;
    }

    setMessage("eval() ran");
}

From source file:org.aw20.mongoworkbench.command.FindMongoCommand.java

License:Open Source License

@Override
public void execute() throws Exception {
    MongoClient mdb = MongoFactory.getInst().getMongo(sName);
    if (mdb == null)
        throw new Exception("no server selected");

    if (sDb == null)
        throw new Exception("no database selected");

    MongoFactory.getInst().setActiveDB(sDb);

    DB db = mdb.getDB(sDb);
    cmdMap = parseMongoCommandString(db, cmd);

    DBCollection collection = db.getCollection(sColl);

    // Construct the query
    int argSize = ((List) cmdMap.get("findArg")).size();

    skip = getSkip();/*  ww  w .  j ava 2 s  .c om*/
    limit = getLimit();

    if (argSize == 0) {
        cursor = collection.find();
    } else if (argSize == 1) {
        DBObject ref = (DBObject) ((List) cmdMap.get("findArg")).get(0);
        cursor = collection.find(ref);
    } else if (argSize >= 2) {
        List findArg = (List) cmdMap.get("findArg");
        DBObject ref = (DBObject) findArg.get(0);
        DBObject key = (DBObject) findArg.get(1);
        cursor = collection.find(ref, key);
    }

    // Apply the skip/limit
    if (skip > -1)
        cursor = cursor.skip(skip);
    if (limit > -1)
        cursor = cursor.limit(limit);

    // Apply the sort
    if (cmdMap.containsField("sortArg")) {
        cursor = cursor.sort((BasicDBObject) cmdMap.get("sortArg"));
    }

    count = cursor.count();

    setMessage("count=" + count + ((skip > -1) ? ("; skip=" + skip) : "")
            + ((limit > -1) ? ("; limit=" + limit) : ""));
}

From source file:org.aw20.mongoworkbench.command.FindOneMongoCommand.java

License:Open Source License

@Override
public void execute() throws Exception {
    MongoClient mdb = MongoFactory.getInst().getMongo(sName);
    if (mdb == null)
        throw new Exception("no server selected");

    if (sDb == null)
        throw new Exception("no database selected");

    MongoFactory.getInst().setActiveDB(sDb);

    DB db = mdb.getDB(sDb);
    DBObject cmdMap = parseMongoCommandString(db, cmd);

    DBCollection collection = db.getCollection(sColl);

    // Construct the query
    int argSize = ((List) cmdMap.get("findOneArg")).size();

    if (argSize == 1) {
        DBObject ref = (DBObject) ((List) cmdMap.get("findOneArg")).get(0);
        dbo = collection.findOne(ref);//  w w w.j av  a2s .  c o m
    } else if (argSize >= 2) {
        List findArg = (List) cmdMap.get("findOneArg");
        DBObject ref = (DBObject) findArg.get(0);
        DBObject key = (DBObject) findArg.get(1);
        dbo = collection.findOne(ref, key);
    } else
        throw new Exception("no query specified");

    setMessage("loaded: " + (dbo != null));
}

From source file:org.aw20.mongoworkbench.command.GridFSCreateBucketCommand.java

License:Open Source License

@Override
public void execute() throws Exception {
    MongoClient mdb = MongoFactory.getInst().getMongo(sName);

    if (mdb == null)
        throw new Exception("no server selected");

    if (sDb == null)
        throw new Exception("no database selected");

    MongoFactory.getInst().setActiveDB(sDb);
    DB db = mdb.getDB(sDb);

    GridFS gfs = new GridFS(db, sColl);
    gfs.getBucketName();/*w w w .j  ava 2 s. c  om*/

    setMessage("bucketCreated=" + sColl);
}

From source file:org.aw20.mongoworkbench.command.GridFSGetFileCommand.java

License:Open Source License

@Override
public void execute() throws Exception {
    MongoClient mdb = MongoFactory.getInst().getMongo(sName);

    if (mdb == null)
        throw new Exception("no server selected");

    if (sDb == null)
        throw new Exception("no database selected");

    MongoFactory.getInst().setActiveDB(sDb);
    DB db = mdb.getDB(sDb);

    GridFS gfs = new GridFS(db, sColl.substring(0, sColl.lastIndexOf(".")));
    GridFSDBFile gridFSDBFile = gfs.find(id);
    gridFSDBFile.writeTo(saveFile);// w ww  .  j  a  v a 2 s  .co  m

    setMessage("fileSaved=" + saveFile + "; size=" + saveFile.length());
}