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.MongoTestInStmt.java

License:Open Source License

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

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

    Integer[] inCondition = { 1, 2 };
    BasicDBObject inQuery = new BasicDBObject();
    inQuery.put("rental_id", new BasicDBObject("$in", inCondition));

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

    mongo.close();

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

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

License:Open Source License

/**
 * @param args/*w  ww . ja  v a2  s.com*/
 */
public static void main(String[] args) throws Exception {
    System.out.println("select * from language where name like '%en%'");
    ConAndAuthentication testMongoCls = new ConAndAuthentication();
    Mongo mongo = testMongoCls.connection(ConAndAuthentication.serverurl, ConAndAuthentication.port);
    DB db = mongo.getDB("test");

    DBCollection myColl = db.getCollection("language");
    BasicDBObject dbObject = new BasicDBObject();
    Pattern regex = Pattern.compile(".*en*");
    dbObject.put("name", regex);

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

    mongo.close();
}

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

License:Open Source License

/**
 * @param args/*w  ww.j a  va2s  . 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("listCommands", 1);
    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]" + JSONUtil.getPretty(cr.toString()));
    System.out.println("[size]" + cr.size());

    mongo.close();
}

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

License:Open Source License

/**
 * @param args//from   www  . j  a  v  a  2s.  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("person");

    MapReduceOutput out = coll.mapReduce(map, reduce, null, MapReduceCommand.OutputType.INLINE, null);
    for (DBObject obj : out.results()) {
        System.out.println("======================================================");
        System.out.println("\t[_id]\t" + obj.get("_id"));
        Map objResult = (Map) obj.get("value");
        System.out.println("\t[count]\t" + objResult.get("count"));
        System.out.println("\t[sum]\t" + objResult.get("sum"));

        System.out.println(obj);
    }
    out.getRaw();
    out.getRaw();

    mongo.close();

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

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

License:Open Source License

/**
 * @param args//  ww  w .  j  av a2s.  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 dbObject = (DBObject) JSON.parse("{capped:true, size:100000}");
    DBCollection dbColl = db.createCollection("newCollection3", dbObject);

    //      db.cappedcoll.insert({"names": {"First": "Gonza", "Last": "Vieira"}}); 
    //      db.cappedcoll.update({"names.First": "Gonza"},{$unset: {"_id":1}});

    mongo.close();
}

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

License:Open Source License

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

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

    //      BasicDBObject myAndQuery = new BasicDBObject();
    //      myAndQuery.append("rental_id", new BasicDBObject("$ne", 1));

    BasicDBObject basicFields = new BasicDBObject();
    BasicDBObject basicWhere = new BasicDBObject();
    BasicDBObject basicSort = new BasicDBObject();

    DBCursor myCursor = myColl.find(basicFields, basicWhere).sort(basicSort).limit(999);
    while (myCursor.hasNext()) {
        System.out.println(myCursor.next());
    }

    mongo.close();
}

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

License:Open Source License

/**
 * @param args/* w w  w  .  j  av  a  2 s.  c o  m*/
 */
public static void main(String[] args) throws Exception {
    System.out.println("select * from rental where rental_id <=5 or rental_id =2;");
    ConAndAuthentication testMongoCls = new ConAndAuthentication();
    Mongo mongo = testMongoCls.connection(ConAndAuthentication.serverurl, ConAndAuthentication.port);
    DB db = mongo.getDB("test");

    DBCollection myColl = db.getCollection("rental");
    ArrayList<BasicDBObject> myList = new ArrayList<BasicDBObject>();
    myList.add(new BasicDBObject("rental_id", new BasicDBObject("$lte", 5)));
    myList.add(new BasicDBObject("rental_id", 2));

    BasicDBObject myOrQuery = new BasicDBObject("$or", myList);

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

    mongo.close();
}

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

License:Open Source License

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

    // ??       
    System.out.println("####[profilling  ]######################################################");
    CommandResult cr = db.command(new BasicDBObject("profile", 0));

    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());
    System.out.println("####[profilling  ]######################################################");

    //  ?          
    System.out.println(
            "####[profilling collections  ]######################################################");
    if (db.collectionExists("system.profile")) {
        DBCollection profileColl = db.getCollection("system.profile");
        profileColl.drop();
    }
    System.out.println(
            "####[profilling collections  ]######################################################");
    //  ?     

    // system.profile collection ? 
    System.out.println(
            "####[profilling collections ? ]######################################################");
    DBObject dbObject = (DBObject) JSON.parse("{capped:true, size:8000000}");
    DBCollection dbColl = db.createCollection("system.profile", dbObject);
    BasicDBObject newProfileColl = (BasicDBObject) dbColl.getStats().copy();
    System.out.println(
            "####[profilling collections ? ]######################################################");
    // system.profile collection ? 

    System.out.println("####[profilling ]######################################################");
    cr = db.command(new BasicDBObject("profile", 2));

    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());
    System.out.println("####[profilling ]######################################################");

    //      //#######################################################################################################
    //      //#######################################################################################################
    //      //#######################################################################################################      
    System.out.println("####[start profilling result]######################################################");
    DBCollection myColl = db.getCollection("system.profile");

    BasicDBObject query = new BasicDBObject();
    query.put("millis", new BasicDBObject("$gt", 4));

    DBCursor myCursor = myColl.find();
    while (myCursor.hasNext()) {
        DBObject dbObj = myCursor.next();
        System.out.println(dbObj.get("ts") + " - " + dbObj.get("ns") + " - " + dbObj.get("nscanned") + "/"
                + dbObj.get("nreturned") + " millis :" + dbObj.get("millis"));
    }
    System.out.println("####[end profilling result]######################################################");

    mongo.close();
}

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

License:Open Source License

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

    DBObject colInformation = (DBObject) JSON.parse("{capped:true, size:100000}");
    DBCollection ref1Coll = db.getCollection(REF_1);
    if (ref1Coll != null)
        ref1Coll.drop();
    ref1Coll = db.createCollection(REF_1, colInformation);

    DBObject dbObjRef1 = (DBObject) JSON.parse("{ 'name' : 'cho'}");//"{'names': {'First': 'Gonza', 'Last': 'Vieira'}}");
    WriteResult wr = ref1Coll.insert(dbObjRef1);

    DBObject retDBObj = ref1Coll.findOne();
    createRef1Collection(db, retDBObj.get("_id"));

    mongo.close();
}

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

License:Open Source License

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

    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();
}