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:com.hangum.tadpole.mongodb.core.test.ConvertJsonToDBObject.java

License:Open Source License

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");

    DBCollection myColl = db.getCollection("objectInsert");

    //      /*from ww w.  java 2  s .c  o m*/
    //      DBObject dbObject = new BasicDBObject();
    //      dbObject.put("aa", 1);
    //      dbObject.put("bb", "33");

    DBObject dbObject = (DBObject) JSON.parse("{'rental_id':1,  'inventory_id':367}");
    myColl.insert(dbObject);

    DBCursor cursorDoc = myColl.find();
    while (cursorDoc.hasNext()) {
        System.out.println(cursorDoc.next());
    }

    System.out.println("Done");

}

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

License:Open Source License

/**
 * @param args/*from  ww w  . j av  a  2  s  .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("test");

    boolean bool = db.authenticate("admin", "admin".toCharArray());
    System.out.println(bool);

    //      WriteResult wr = db.addUser("admin", "admin".toCharArray());
    //      System.out.println("[add user]" +  wr.getError() );

    //      CommandResult cr = db.getStats();
    //         
    //      System.out.println("[ok]" + cr.ok() );
    //      if(!cr.ok()) System.out.println("[Exception ]" + cr.getException().toString());
    //      System.out.println("[toString]" + JSONUtil.getPretty(cr.toString()));
    //      System.out.println("[size]" + cr.size() );

    mongo.close();
}

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

License:Open Source License

/**
 * @param args//from ww w .  ja v a 2 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");

    DBCollection myColl = db.getCollection("rental");

    BasicDBObject mainQuery = new BasicDBObject();

    // tmp and
    BasicDBObject tmpAndQuery = new BasicDBObject();
    tmpAndQuery.append("inventory_id", 100);
    tmpAndQuery.append("rental_id", new BasicDBObject("$ne", 1));

    mainQuery.put("$and", tmpAndQuery);

    // tmp or
    ArrayList<BasicDBObject> myList = new ArrayList<BasicDBObject>();
    myList.add(new BasicDBObject("customer_id", 3));

    mainQuery.put("$or", myList);

    System.out.println(mainQuery.toString());

    DBCursor myCursor = myColl.find(mainQuery);
    System.out.println("[result cursor size is " + myCursor.count());
    while (myCursor.hasNext()) {
        System.out.println(myCursor.next());
    }

    mongo.close();
}

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

License:Open Source License

/**
 * @param args/* w  w  w  .ja v a2s  .  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");

    DBCollection myColl = db.getCollection("city1");

    // show column
    BasicDBObject myColumn = new BasicDBObject();
    myColumn.put("loc.y", true);

    // where
    BasicDBObject myAndQuery = new BasicDBObject();
    //      myAndQuery.append("rental_id", new BasicDBObject("$gte", 1));
    //      myAndQuery.append("inventory_id", 367);//new BasicDBObject("$eq", 367));

    DBCursor myCursor = myColl.find(myAndQuery, myColumn);
    while (myCursor.hasNext()) {
        System.out.println(myCursor.next());
    }

    mongo.close();
}

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

License:Open Source License

/**
 * @param args/*  ww  w.  j  a va2s . 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("newdb2");
    Set<String> listColNames = db.getCollectionNames();
    for (String string : args) {
        System.out.println("[collection name]" + string);
    }

    //      DBObject dbObject = (DBObject) JSON.parse("{capped:true, size:100000}");
    //      db.createCollection("test", dbObject);

    mongo.close();
}

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

License:Open Source License

/**
 * @param args/*from  www  .  j a  v a2  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 dbObj = (DBObject) db.eval("db.currentOp()");
    System.out.println(JSONUtil.getPretty(dbObj.toString()));

    BasicDBList dbInprogs = (BasicDBList) dbObj.get("inprog");
    for (Object object : dbInprogs) {
        BasicDBObject obj = (BasicDBObject) object;
        System.out.println("[opid]" + obj.get("opid"));

        //         System.out.println("##[start] killOp##############");
        //         db.eval("db.killOp(" + obj.get("opid") + ")");
        //         System.out.println("##[stop] killOp##############");
    }

    mongo.close();
}

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

License:Open Source License

/**
 * @param args/*from  w  ww  .  ja  va2 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");

    CommandResult cr = db.getStats();

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

    mongo.close();
}

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

License:Open Source License

/**
 * @param args/*from  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("test");

    //      saveImage(db);
    //      getImage(db);
    allList(db);

    mongo.close();
}

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

License:Open Source License

/**
 * @param args// w  w  w.  j a  va 2  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");
    DBCollection coll = db.getCollection("group");

    DBObject dbObjectKey = (DBObject) JSON.parse(key);
    DBObject dbObjectInitial = (DBObject) JSON.parse(initial);
    DBObject dbObjectCondition = (DBObject) JSON.parse(condition);

    DBObject resultDBObject = coll.group(dbObjectKey, dbObjectCondition, dbObjectInitial, reduce, finalizer);
    System.out.println(resultDBObject);

    mongo.close();

    try {
        Thread.sleep(1);
    } catch (Exception e) {
    }
}

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

License:Open Source License

/**
 * @param args/* www.  j ava 2s  .  c o m*/
 */
public static void main(String[] args) throws Exception {

    //      for(int i=0; i<1000000; i++) {
    ConAndAuthentication testMongoCls = new ConAndAuthentication();
    Mongo mongo = testMongoCls.connection(ConAndAuthentication.serverurl, ConAndAuthentication.port);
    DB db = mongo.getDB("test");

    Set<String> setCollection = db.getCollectionNames();
    for (String colName : setCollection) {
        DBCollection col = db.getCollection(colName);
        List<DBObject> listIndex = col.getIndexInfo();
        System.out.println("[colection]" + colName);
        for (DBObject dbObject : listIndex) {
            System.out.println("\t" + dbObject);
            //               Map<String, Integer> objMap = (Map)dbObject.get("key");
            //               Set<String> objMapKey = objMap.keySet();
            //               for (String strKey : objMapKey) {
            //                  System.out.println("[key]" + strKey + "\t [value]" + objMap.get(strKey).toString());
            //               }
        }
    }

    mongo.close();

    try {
        Thread.sleep(1);
    } catch (Exception e) {
    }
    //      }
}