public List<DBObject> getSourceBrowsers() {

    DBObject groupFields = new BasicDBObject("_id", "$device");
    groupFields.put("sum", new BasicDBObject("$sum", "$sum"));
    DBObject group = new BasicDBObject("$group", groupFields);
    DBObject sort = new BasicDBObject("$sort", new BasicDBObject("sum", 1));

    List<DBObject> pipeline = Arrays.asList(group, sort);

    // allowDiskUse
    AggregationOptions options =
        AggregationOptions.builder().allowDiskUse(true).batchSize(10000).build();
    Cursor cursor = device.aggregate(pipeline, options);

    List<DBObject> list = new ArrayList<DBObject>();
    while (cursor.hasNext()) {
      DBObject object = cursor.next();
      DBObject newObj = new BasicDBObject();
      newObj.put("device", object.get("_id"));
      newObj.put("sum", object.get("sum"));
      list.add(newObj);
    }
    cursor.close();
    return list;
  }
Exemple #2
0
    public static void main(String[] args) throws UnknownHostException {
        MongoClient client = new MongoClient();
        DB db = client.getDB("hw31");
        DBCollection collection = db.getCollection("students");

        // create our pipeline operations, first with the $match
        DBObject match = new BasicDBObject("$match", new BasicDBObject("scores.type", "homework") );

        // $unwind op
        //DBObject unwind = new BasicDBObject("$unwind", "$scores");

        // Now the $group operation
        DBObject groupFields = new BasicDBObject( "_id", "$student_id");
        //groupFields.put("score", new BasicDBObject( "$min", "scores.$score"));
        DBObject group = new BasicDBObject("$group", groupFields);
//
//        // $sort operation
//        DBObject sortFields = new BasicDBObject("_id", 1).append("score", 1);
//        DBObject sort = new BasicDBObject("$sort", sortFields);

        // run aggregation
        AggregationOutput output = collection.aggregate(match, group);

        for(Iterator<DBObject> i = output.results().iterator(); i.hasNext();) {
            DBObject result = i.next();
            System.out.println(result);
//            collection.remove(QueryBuilder.start("student_id").is(result.get("_id"))
//                    .and("score").is(result.get("score")).and("type").is("homework").get());
        }

        System.out.println(collection.count());
    }
  private List<String> selectViewCodes(String schema) throws UnknownHostException {
    // Initiate variables
    List<String> l = new ArrayList<String>();
    MongoDBConnectionManager mgr = MongoDBConnectionManager.getInstance();
    Mongo mongo = mgr.getMongo();
    DB db = mongo.getDB(schema);
    DBCollection collection = db.getCollection("views");

    // Compose NoSQL query
    BasicDBObject groupFiels = new BasicDBObject("_id", "$view_id");
    BasicDBObject group = new BasicDBObject("$group", groupFiels);
    BasicDBObject sortFiels = new BasicDBObject("_id", 1);
    BasicDBObject sort = new BasicDBObject("$sort", sortFiels);

    // Create the output
    AggregationOutput output = collection.aggregate(group, sort);
    Iterable<DBObject> results = output.results();
    for (DBObject result : results) {
      try {
        l.add(result.get("_id").toString());
      } catch (NullPointerException e) {
        System.out.println("Skipping NULL");
      }
    }

    // Return the result
    return l;
  }
  // 获得一天的转换漏斗
  public List<DBObject> getConversionFunnel(String date) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    DBObject match = null;
    try {
      match = new BasicDBObject("$match", new BasicDBObject("date", sdf.parse(date)));
    } catch (ParseException e) {

    }
    DBObject groupFields = new BasicDBObject("_id", "");
    groupFields.put("step1sum", new BasicDBObject("$sum", "$step1"));
    groupFields.put("step2sum", new BasicDBObject("$sum", "$step2"));
    groupFields.put("step3sum", new BasicDBObject("$sum", "$step3"));

    DBObject group = new BasicDBObject("$group", groupFields);
    // DBObject sort = new BasicDBObject("$sort", new BasicDBObject("sum", 1));

    List<DBObject> pipeline = Arrays.asList(match, group);

    // allowDiskUse
    AggregationOptions options =
        AggregationOptions.builder().allowDiskUse(true).batchSize(10000).build();
    Cursor cursor = CF.aggregate(pipeline, options);

    List<DBObject> list = new ArrayList<DBObject>();
    while (cursor.hasNext()) {
      DBObject object = cursor.next();
      DBObject newObj = new BasicDBObject();
      //            DBObject _id = (DBObject)object.get("_id");
      //            newObj.put("date",sdf.format(_id.get("$date")));
      DBObject newObj1 = new BasicDBObject();
      newObj1.put("step", "访问本站");
      newObj1.put("num", object.get("step1sum"));
      list.add(newObj1);
      DBObject newObj2 = new BasicDBObject();
      newObj2.put("step", "浏览商品");
      newObj2.put("num", object.get("step2sum"));
      list.add(newObj2);

      DBObject newObj3 = new BasicDBObject();
      newObj3.put("step", "完成交易");
      newObj3.put("num", object.get("step3sum"));
      list.add(newObj3);
    }
    cursor.close();
    return list;
  }