Example usage for com.mongodb Mongo close

List of usage examples for com.mongodb Mongo close

Introduction

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

Prototype

public void close() 

Source Link

Document

Closes all resources associated with this instance, in particular any open network connections.

Usage

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

License:Open Source License

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

From source file:com.hangum.tadpole.mongodb.core.test.MongoTestInStmt.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 {

    //      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//from   ww w.  j ava 2  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// www  . j a v a  2  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");

    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/*  w w w .j  av  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 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//w  w w .  j  ava2 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 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//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");

    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/*from  w w w .  j  a va2s.c  om*/
 */
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/*from ww w.  j  av a2  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");

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