Example usage for com.mongodb DBCollection find

List of usage examples for com.mongodb DBCollection find

Introduction

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

Prototype

public DBCursor find(final DBObject query) 

Source Link

Document

Select documents in collection and get a cursor to the selected documents.

Usage

From source file:com.glaf.wechat.mongodb.service.impl.WxMongoDBLogServiceImpl.java

License:Apache License

public List<WxLog> list(WxLogQuery query) {
    DB db = mongoTemplate.getDb();/*w  w  w .  ja va 2s.c  om*/
    String tableName = "wx_log" + query.getSuffix();
    DBCollection coll = db.getCollection(tableName);
    BasicDBObject q = new BasicDBObject();
    this.fillQueryCondition(q, query);
    List<DBObject> list = coll.find(q).toArray();
    List<WxLog> logs = new java.util.concurrent.CopyOnWriteArrayList<WxLog>();
    for (DBObject object : list) {
        WxLog log = new WxLog();
        log.setId((Long) object.get("id"));
        log.setIp((String) object.get("ip"));
        log.setActorId((String) object.get("actorId"));
        log.setOperate((String) object.get("operate"));
        log.setContent((String) object.get("content"));
        if (object.containsField("accountId")) {
            log.setAccountId((Long) object.get("accountId"));
        }
        if (object.containsField("openId")) {
            log.setOpenId((String) object.get("openId"));
        }
        if (object.containsField("flag")) {
            log.setFlag((Integer) object.get("flag"));
        }
        if (object.containsField("createTime")) {
            long ts = (Long) object.get("createTime");
            log.setCreateTime(new Date(ts));
        }

        logs.add(log);
    }
    return logs;
}

From source file:com.google.api.ads.adwords.jaxws.extensions.report.model.persistence.mongodb.MongoEntityPersister.java

License:Open Source License

/**
 * @see com.google.api.ads.adwords.jaxws.extensions.report.model.persistence.EntityPersister
 *      #get(java.lang.Class, java.util.Map, int, int)
 *///from w w  w  . j  av a 2 s  . c o m
@Override
public <T, V> List<T> get(Class<T> t, Map<String, V> keyValueList, int numToSkip, int limit) {
    DBCollection dbcollection = getDBCollection(t, true);

    BasicDBObject query = new BasicDBObject();
    if (keyValueList != null) {
        for (String key : keyValueList.keySet()) {
            query.put(key, keyValueList.get(key));
        }
    }
    DBCursor cur = dbcollection.find(query);

    if (limit > 0) {
        cur.limit(limit);
    }
    if (numToSkip > 0) {
        cur.skip(numToSkip);
    }

    List<T> list = new ArrayList<T>();
    while (cur.hasNext()) {
        DBObject dbObject = cur.next();
        list.add(gson.fromJson(com.mongodb.util.JSON.serialize(dbObject), t));
    }
    return list;
}

From source file:com.google.api.services.samples.analytics.cmdline.CoreReportingApiReferenceSample.java

License:Apache License

private static void insertVisitAttributesData(GaData gaData, DBCollection collection, Date d,
        DBCollection centerMapping) throws JSONException {
    if (gaData.getTotalResults() > 0) {
        System.out.println("Data Table:" + collection);

        String[] columns = (VISIT_ATTRIBUTES_METRICS + "," + VISIT_ATTRIBUTES_DIMENSIONS).split(",");
        HashMap<String, Integer> columnLookUp = new HashMap<String, Integer>();
        List<ColumnHeaders> columnHeaders = gaData.getColumnHeaders();
        for (String column : columns) {
            for (int i = 0; i < columnHeaders.size(); i++) {
                if (columnHeaders.get(i).getName().equals(column)) {
                    columnLookUp.put(column, i);
                    break;
                }/* w  w  w .j a v a  2  s  . c om*/
            }
        }

        if (!gaData.getContainsSampledData()) {
            for (List<String> rowValues : gaData.getRows()) {
                String demandBaseId = rowValues.get(columnLookUp.get("ga:dimension11"));
                String clientId = rowValues.get(columnLookUp.get("ga:dimension2"));
                String pagePath = rowValues.get(columnLookUp.get("ga:pagePath"));
                String source = rowValues.get(columnLookUp.get("ga:source"));
                String medium = rowValues.get(columnLookUp.get("ga:medium"));
                //                    String visits = rowValues.get(columnLookUp.get("ga:visits"));
                //                    String users = rowValues.get(columnLookUp.get("ga:users"));
                //                    String pageViews = rowValues.get(columnLookUp.get("ga:pageviews"));
                //                    String sessionDuration = rowValues.get(columnLookUp.get("ga:sessionDuration"));

                HashMap<Object, Object> map = new HashMap<Object, Object>();
                map.put("demandbase_sid", demandBaseId);
                map.put("clientId", clientId);
                String[] split = pagePath.split("\\?"); // remove all characters after the URL parameters
                String[] withoutMobileUrl = split[0].split("regus.com");
                String strippedPagePath = withoutMobileUrl[withoutMobileUrl.length - 1];

                String product = "", centerLookUp = "", centerId = "";
                String[] locations = strippedPagePath.split("locations/");
                if (locations.length > 1) {
                    int index = locations[1].indexOf("/");
                    product = locations[1].substring(0, index);
                    centerLookUp = locations[1].substring(index + 1);

                    HashMap<Object, Object> centerLookUpMap = new HashMap<Object, Object>();
                    centerLookUpMap.put("CentreURLName", centerLookUp);
                    BasicDBObject objectToRemove = new BasicDBObject(centerLookUpMap);
                    DBCursor cursor = centerMapping.find(objectToRemove);
                    if (cursor.hasNext()) {
                        centerId = cursor.next().get("CentreID").toString();
                    }

                }
                map.put("pagePath", strippedPagePath);
                map.put("source", source);
                map.put("medium", medium);
                map.put("product", product);
                map.put("centerId", centerId);
                //                    map.put("visits", visits);
                //                    map.put("users", users);
                //                    map.put("pageViews", pageViews);
                //                    map.put("sessionDuration", sessionDuration);
                map.put("date", new SimpleDateFormat("yyyy/MM/dd").format(d));
                BasicDBObject objectToInsert = new BasicDBObject(map);
                collection.insert(objectToInsert);
            }
        } else {
            System.out.println(" Excluding analytics data since it has sample data");
        }
    } else {
        System.out.println("No data");
    }
}

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

License:Open Source License

/**
 * @param args//from w  w w. ja  v  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  a v a 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.MongoTestORStmt.java

License:Open Source License

/**
 * @param args/*from   w w w. j  a v  a2  s . co  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.MongoTestRunCommand.java

License:Open Source License

/**
 * @param args//w  w  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, 27017);//ConAndAuthentication.port);
    DB db = mongo.getDB("test");

    DBCollection dbColl = db.getCollection("user");

    DBObject cmdObj = (DBObject) JSON.parse("{language  : 'en_us'}, {seq: true, email:true}, {seq, -1}");
    DBCursor dbCur = dbColl.find(cmdObj);
    for (DBObject obj : dbCur.toArray()) {
        System.out.println(obj);
    }

    //      CommandResult cr = db.command(cmdObj);//new BasicDBObject("create", "hyunjong"));
    //      System.out.println( cr.toString() );      

    mongo.close();
}

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

License:Open Source License

/**
 * @param args//  w ww .  j ava2s . com
 */
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 collAddress = db.getCollection("test555");

    BasicDBObject findObj = new BasicDBObject().append("n", 2);
    DBCursor cur = collAddress.find(findObj);
    DBObject dbObj = cur.next();
    System.out.println(dbObj);
    System.out.println("================================================================================");

    if (dbObj != null) {
        BasicDBObject newDocument3 = new BasicDBObject().append("$set",
                new BasicDBObject().append("allPlans.0.cursor", "t2est"));
        //  allPlans.0.cursor
        WriteResult wr = collAddress.update(dbObj, newDocument3);
    }
    //
    //      System.out.println(wr.toString());
    //      
    mongo.close();
}

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

License:Open Source License

public static void exam05(DBCollection collection) throws Exception {
    // find type = vps , update all matched documents , "clients" value to
    // 888//  ww  w  .  j  a  v  a2s  .c o m
    BasicDBObject updateQuery = new BasicDBObject().append("$set",
            new BasicDBObject().append("clients", "11111"));

    BasicDBObject findObj = new BasicDBObject().append("hosting", "hostA");
    DBObject dbObj = collection.find(findObj).next();
    System.out.println(dbObj);

    // both methods are doing the same thing.
    // collection.updateMulti(new BasicDBObject().append("type", "vps"),
    // updateQuery);
    collection.update(dbObj, updateQuery);
}