Example usage for com.mongodb DBCursor hasNext

List of usage examples for com.mongodb DBCursor hasNext

Introduction

In this page you can find the example usage for com.mongodb DBCursor hasNext.

Prototype

@Override
public boolean hasNext() 

Source Link

Document

Checks if there is another object available.

Usage

From source file:com.groupon.jenkins.mongo.MongoRepository.java

License:Open Source License

protected <T> Iterable<T> find(DBObject query, DBObject fields, DBObject sort, Integer limit,
        Function<DBObject, T> transformer) {
    MongoClient client = getClient();/*from  w ww .  ja v  a 2  s.  c  om*/
    try {
        DBCursor cursor = fields == null ? getCollection(client).find(query)
                : getCollection(client).find(query, fields);
        if (sort != null) {
            cursor = cursor.sort(sort);
        }
        if (limit != null) {
            cursor = cursor.limit(limit);
        }
        List<T> result = new LinkedList<T>();

        try {
            while (cursor.hasNext()) {
                result.add(transformer.apply(cursor.next()));
            }
        } finally {
            cursor.close();
        }

        return result;
    } finally {
        client.close();
    }
}

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

    //      //  www .  j a va  2 s. c  om
    //      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.MongoTestAndORComplexStmt.java

License:Open Source License

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

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

License:Open Source License

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

    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 w  w w .jav  a2  s.  c  o  m*/
 */
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.MongoTestNotEqualsStmt.java

License:Open Source License

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

    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   ww  w .j  a v  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  .  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");

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