예제 #1
0
  /** Creates each document for pipeline with instances of Bson documents */
  private void fromBsonDocuments() {

    Document totalPop =
        new Document()
            .append(
                "$group",
                new Document()
                    .append("_id", "$state")
                    .append("totalPop", new Document("$sum", "$pop")));

    Document gtOneMillion =
        new Document().append("$match", new Document("totalPop", new Document("$gte", 1000000)));

    //
    // Compose aggregation pipeline
    List<Document> pipeline = Arrays.asList(totalPop, gtOneMillion);

    //
    // Executes aggregation query
    List<Document> results =
        DBHelper.getZipCodesCollection().aggregate(pipeline).into(new ArrayList<Document>());

    //
    // Display aggregation results
    System.out.println("\nStates with population greater than one million");
    for (Document result : results) {
      System.out.println(result.toJson());
    }
  }
예제 #2
0
  /** Creates each document for pipeline using helper classes included in driver */
  private void fromHelperClasses() {

    Bson totalPop = Aggregates.group("$state", Accumulators.sum("totalPop", "$pop"));
    Bson gtOneMillion = Aggregates.match(Filters.gte("totalPop", 1000000));

    //
    // Compose aggregation pipeline
    List<Bson> pipeline = Arrays.asList(totalPop, gtOneMillion);

    //
    // Executes aggregation query
    List<Document> results =
        DBHelper.getZipCodesCollection().aggregate(pipeline).into(new ArrayList<Document>());

    //
    // Display aggregation results
    System.out.println("\nStates with population greater than one million");
    for (Document result : results) {
      System.out.println(result.toJson());
    }
  }
예제 #3
0
  /** Parse a raw string into json for the aggregation framework */
  private void fromRawString() {

    String totalPop = "{ $group: { _id: \"$state\", totalPop: { $sum: \"$pop\"}}}";
    String gtOneMillion = "{ $match: { totalPop: {$gte: 1000000}}}";

    //
    // Compose aggregation pipeline
    List<Document> pipeline = Arrays.asList(Document.parse(totalPop), Document.parse(gtOneMillion));

    //
    // Executes aggregation query
    List<Document> results =
        DBHelper.getZipCodesCollection().aggregate(pipeline).into(new ArrayList<Document>());

    //
    // Display aggregation results
    System.out.println("\nStates with population greater than one million");
    for (Document result : results) {
      System.out.println(result.toJson());
    }
  }