Java 类com.mongodb.client.model.Accumulators 实例源码

项目:Backend    文件:MongoDBManager.java   
public int returnValueToURL(String URL)
{
    Block<Document> printBlock = new Block<Document>() {
        @Override
        public void apply(final Document document) {
            System.out.println(document.toJson());
        }
    };
    MongoCollection<Document> collection = db.getCollection("ratings");

    collection.aggregate(
            Arrays.asList(
                    Aggregates.group("URL", Accumulators.avg("rating", 1))))
            .forEach(printBlock);
    System.out.println(printBlock.toString());
    return 0;
}
项目:digital-display-garden-iteration-3-sixguysburgers-fries    文件:MongoSpec.java   
@Test
public void ageCounts() {
    AggregateIterable<Document> documents
            = userDocuments.aggregate(
            Arrays.asList(
                    /*
                     * Groups data by the "age" field, and then counts
                     * the number of documents with each given age.
                     * This creates a new "constructed document" that
                     * has "age" as it's "_id", and the count as the
                     * "ageCount" field.
                     */
                    Aggregates.group("$age",
                            Accumulators.sum("ageCount", 1)),
                    Aggregates.sort(Sorts.ascending("_id"))
            )
    );
    List<Document> docs = intoList(documents);
    assertEquals("Should be two distinct ages", 2, docs.size());
    assertEquals(docs.get(0).get("_id"), 25);
    assertEquals(docs.get(0).get("ageCount"), 1);
    assertEquals(docs.get(1).get("_id"), 37);
    assertEquals(docs.get(1).get("ageCount"), 2);
}
项目:digital-display-garden-iteration-3-sixguysburgers-fries    文件:MongoSpec.java   
@Test
public void averageAge() {
    AggregateIterable<Document> documents
            = userDocuments.aggregate(
                    Arrays.asList(
                            Aggregates.group("$company",
                                    Accumulators.avg("averageAge", "$age")),
                            Aggregates.sort(Sorts.ascending("_id"))
                    ));
    List<Document> docs = intoList(documents);
    assertEquals("Should be three companies", 3, docs.size());

    assertEquals("Frogs, Inc.", docs.get(0).get("_id"));
    assertEquals(37.0, docs.get(0).get("averageAge"));
    assertEquals("IBM", docs.get(1).get("_id"));
    assertEquals(37.0, docs.get(1).get("averageAge"));
    assertEquals("UMM", docs.get(2).get("_id"));
    assertEquals(25.0, docs.get(2).get("averageAge"));
}
项目:digital-display-garden-iteration-4-dorfner-v2    文件:MongoSpec.java   
@Test
public void ageCounts() {
    AggregateIterable<Document> documents
            = userDocuments.aggregate(
            Arrays.asList(
                    /*
                     * Groups data by the "age" field, and then counts
                     * the number of documents with each given age.
                     * This creates a new "constructed document" that
                     * has "age" as it's "_id", and the count as the
                     * "ageCount" field.
                     */
                    Aggregates.group("$age",
                            Accumulators.sum("ageCount", 1)),
                    Aggregates.sort(Sorts.ascending("_id"))
            )
    );
    List<Document> docs = intoList(documents);
    assertEquals("Should be two distinct ages", 2, docs.size());
    assertEquals(docs.get(0).get("_id"), 25);
    assertEquals(docs.get(0).get("ageCount"), 1);
    assertEquals(docs.get(1).get("_id"), 37);
    assertEquals(docs.get(1).get("ageCount"), 2);
}
项目:digital-display-garden-iteration-4-dorfner-v2    文件:MongoSpec.java   
@Test
public void averageAge() {
    AggregateIterable<Document> documents
            = userDocuments.aggregate(
                    Arrays.asList(
                            Aggregates.group("$company",
                                    Accumulators.avg("averageAge", "$age")),
                            Aggregates.sort(Sorts.ascending("_id"))
                    ));
    List<Document> docs = intoList(documents);
    assertEquals("Should be three companies", 3, docs.size());

    assertEquals("Frogs, Inc.", docs.get(0).get("_id"));
    assertEquals(37.0, docs.get(0).get("averageAge"));
    assertEquals("IBM", docs.get(1).get("_id"));
    assertEquals(37.0, docs.get(1).get("averageAge"));
    assertEquals("UMM", docs.get(2).get("_id"));
    assertEquals(25.0, docs.get(2).get("averageAge"));
}
项目:digital-display-garden-iteration-2-spraguesanborn    文件:MongoSpec.java   
@Test
public void ageCounts() {
    AggregateIterable<Document> documents
            = userDocuments.aggregate(
            Arrays.asList(
                    /*
                     * Groups data by the "age" field, and then counts
                     * the number of documents with each given age.
                     * This creates a new "constructed document" that
                     * has "age" as it's "_id", and the count as the
                     * "ageCount" field.
                     */
                    Aggregates.group("$age",
                            Accumulators.sum("ageCount", 1)),
                    Aggregates.sort(Sorts.ascending("_id"))
            )
    );
    List<Document> docs = intoList(documents);
    assertEquals("Should be two distinct ages", 2, docs.size());
    assertEquals(docs.get(0).get("_id"), 25);
    assertEquals(docs.get(0).get("ageCount"), 1);
    assertEquals(docs.get(1).get("_id"), 37);
    assertEquals(docs.get(1).get("ageCount"), 2);
}
项目:digital-display-garden-iteration-2-spraguesanborn    文件:MongoSpec.java   
@Test
public void averageAge() {
    AggregateIterable<Document> documents
            = userDocuments.aggregate(
                    Arrays.asList(
                            Aggregates.group("$company",
                                    Accumulators.avg("averageAge", "$age")),
                            Aggregates.sort(Sorts.ascending("_id"))
                    ));
    List<Document> docs = intoList(documents);
    assertEquals("Should be three companies", 3, docs.size());

    assertEquals("Frogs, Inc.", docs.get(0).get("_id"));
    assertEquals(37.0, docs.get(0).get("averageAge"));
    assertEquals("IBM", docs.get(1).get("_id"));
    assertEquals(37.0, docs.get(1).get("averageAge"));
    assertEquals("UMM", docs.get(2).get("_id"));
    assertEquals(25.0, docs.get(2).get("averageAge"));
}
项目:Liudao    文件:MongoDao.java   
/**
 * 最大统计
 *
 * @param collectionName
 * @param match
 * @param maxField
 * @return
 */
public Object max(String collectionName, Document match, String maxField) {
    AggregateIterable<Document> aggregate = getDB().getCollection(collectionName).aggregate(
            Arrays.asList(
                    match(match)
                    , group(null, Accumulators.max("_max", "$" + maxField))
            )
    );
    Document first = aggregate.first();
    if (first != null) {
        return first.get("_max");
    }
    return null;
}
项目:Liudao    文件:MongoDao.java   
/**
 * 根据统计字段计算统计结果(gte最小值)并排序
 *
 * @param collectionName 集合名
 * @param match          match条件
 * @param field          统计字段
 * @param minCount       最小值
 * @return
 */
public LinkedHashMap<String, Integer> sortMap(String collectionName, Document match, String field, int minCount) {
    AggregateIterable<Document> aggregate = getDB().getCollection(collectionName).aggregate(
            Arrays.asList(
                    match(match)
                    , group("$" + field, Accumulators.sum("_count", 1))
                    , match(new Document("_count", new Document("$gte", minCount)))
                    , sort(new Document("_count", -1))
            )
    );

    LinkedHashMap<String, Integer> map = new LinkedHashMap<String, Integer>();
    MongoCursor<Document> iterator = aggregate.iterator();
    while (iterator.hasNext()) {
        Document next = iterator.next();
        map.put(next.getString("_id"), next.getInteger("_count"));
    }
    return map;
}
项目:digital-display-garden-iteration-2-spraguesanborn    文件:UserController.java   
public String getAverageAgeByCompany() {
    AggregateIterable<Document> documents
            = userCollection.aggregate(
            Arrays.asList(
                    Aggregates.group("$company",
                            Accumulators.avg("averageAge", "$age")),
                    Aggregates.sort(Sorts.ascending("_id"))
            ));
    System.err.println(JSON.serialize(documents));
    return JSON.serialize(documents);
}
项目:Liudao    文件:MongoDao.java   
/**
 * 最小统计
 *
 * @param collectionName
 * @param match
 * @param minField
 * @return
 */
public Object min(String collectionName, Document match, String minField) {
    AggregateIterable<Document> aggregate = getDB().getCollection(collectionName).aggregate(
            Arrays.asList(
                    match(match)
                    , group(null, Accumulators.min("_min", "$" + minField))
            )
    );
    Document first = aggregate.first();
    if (first != null) {
        return first.get("_min");
    }
    return null;
}
项目:Liudao    文件:MongoDao.java   
/**
 * 合统计
 *
 * @param collectionName
 * @param match
 * @param sumField
 * @return
 */
public Double sum(String collectionName, Document match, String sumField) {
    AggregateIterable<Document> aggregate = getDB().getCollection(collectionName).aggregate(
            Arrays.asList(
                    match(match)
                    , group(null, Accumulators.sum("_sum", "$" + sumField))
            )
    );
    Document first = aggregate.first();
    if (first != null) {
        return first.getDouble("_sum");
    }
    return null;
}
项目:Liudao    文件:MongoDao.java   
/**
 * 平均统计
 *
 * @param collectionName
 * @param match
 * @param avgField
 * @return
 */
public Double avg(String collectionName, Document match, String avgField) {
    AggregateIterable<Document> aggregate = getDB().getCollection(collectionName).aggregate(
            Arrays.asList(
                    match(match)
                    , group(null, Accumulators.avg("_avg", "$" + avgField))
            )
    );
    Document first = aggregate.first();
    if (first != null) {
        return first.getDouble("_avg");
    }
    return null;
}
项目:Liudao    文件:MongoDao.java   
/**
 * 最近统计
 *
 * @param collectionName
 * @param match
 * @param lastField
 * @param sort
 * @return
 */
public Object last(String collectionName, Document match, String lastField, Document sort) {
    AggregateIterable<Document> aggregate = getDB().getCollection(collectionName).aggregate(
            Arrays.asList(
                    match(match)
                    , sort(sort)
                    , group(null, Accumulators.last("_last", "$" + lastField))
            )
    );
    Document first = aggregate.first();
    if (first != null) {
        return first.get("_last");
    }
    return null;
}
项目:Liudao    文件:MongoDao.java   
/**
 * 标准差统计
 *
 * @param collectionName
 * @param match
 * @param stdDevField
 * @return
 */
public Double stdDevPop(String collectionName, Document match, String stdDevField) {
    AggregateIterable<Document> aggregate = getDB().getCollection(collectionName).aggregate(
            Arrays.asList(
                    match(match)
                    , group(null, Accumulators.stdDevPop("_stdDev", "$" + stdDevField))
            )
    );
    Document first = aggregate.first();
    if (first != null) {
        return first.getDouble("_stdDev");
    }
    return null;
}
项目:Liudao    文件:MongoDao.java   
/**
 * 采样标准差统计
 *
 * @param collectionName
 * @param match
 * @param stdDevField
 * @param sampleSize
 * @return
 */
public Double stdDevSamp(String collectionName, Document match, String stdDevField, int sampleSize) {
    AggregateIterable<Document> aggregate = getDB().getCollection(collectionName).aggregate(
            Arrays.asList(
                    match(match)
                    , sample(sampleSize)
                    , group(null, Accumulators.stdDevSamp("_stdDev", "$" + stdDevField))
            )
    );
    Document first = aggregate.first();
    if (first != null) {
        return first.getDouble("_stdDev");
    }
    return null;
}
项目:Liudao    文件:MongoDao.java   
/**
 * 统计值 是否在统计结果(gte最小值)中
 *
 * @param collectionName 集合名
 * @param match          match条件
 * @param field          统计字段
 * @param value          统计值
 * @param minCount       最小值
 * @return
 */
public boolean inSortMap(String collectionName, Document match, String field, Object value, int minCount) {
    AggregateIterable<Document> aggregate = getDB().getCollection(collectionName).aggregate(
            Arrays.asList(
                    match(match.append(field, value))
                    , group("$" + field, Accumulators.sum("_count", 1))
                    , match(new Document("_count", new Document("$gte", minCount)))
            )
    );

    Document first = aggregate.first();
    return first == null ? false : true;
}
项目:ibm-performance-monitor    文件:ProfiledMongoClientTest.java   
@Test
public void testAggregate()
{
    List<Document> docList = new ArrayList<Document>();
    coll.aggregate(Arrays.asList(Aggregates.match(Filters.eq("name", "Alto")),
        Aggregates.group("color", Accumulators.sum("count", 1)))).into(docList);

    assertEquals(1, docList.size());

    docList.clear();

    Document first = coll
        .aggregate(Arrays.asList(Aggregates.match(Filters.eq("name", "Alto")),
            Aggregates.group("color", Accumulators.sum("count", 1))), Document.class)
        .allowDiskUse(true).batchSize(12).bypassDocumentValidation(true).collation(Collation.builder().build())
        .first();
    Assert.assertNotNull(first);

    first = coll
        .aggregate(Arrays.asList(Aggregates.match(Filters.eq("name", "Alto")),
            Aggregates.group("color", Accumulators.sum("count", 1))), Document.class)
        .allowDiskUse(true).batchSize(12).bypassDocumentValidation(true).collation(Collation.builder().build())
        .map(new Function<Document, Document>()
        {
            @Override
            public Document apply(Document t)
            {
                t.put("hello", "world");
                return t;
            }
        }).first();
    Assert.assertNotNull(first);

}
项目:MongoDB-Plugin    文件:MongoAccumulator.java   
public MongoAccumulator sum(String fieldName, String expression) {
    accumulators.add(Accumulators.sum(fieldName, expression));
    return this;
}
项目:MongoDB-Plugin    文件:MongoAccumulator.java   
public MongoAccumulator avg(String fieldName, String expression) {
    accumulators.add(Accumulators.avg(fieldName, expression));
    return this;
}
项目:MongoDB-Plugin    文件:MongoAccumulator.java   
public MongoAccumulator first(String fieldName, String expression) {
    accumulators.add(Accumulators.first(fieldName, expression));
    return this;
}
项目:MongoDB-Plugin    文件:MongoAccumulator.java   
public MongoAccumulator last(String fieldName, String expression) {
    accumulators.add(Accumulators.last(fieldName, expression));
    return this;
}
项目:MongoDB-Plugin    文件:MongoAccumulator.java   
public MongoAccumulator max(String fieldName, String expression) {
    accumulators.add(Accumulators.max(fieldName, expression));
    return this;
}
项目:MongoDB-Plugin    文件:MongoAccumulator.java   
public MongoAccumulator min(String fieldName, String expression) {
    accumulators.add(Accumulators.min(fieldName, expression));
    return this;
}
项目:MongoDB-Plugin    文件:MongoAccumulator.java   
public MongoAccumulator push(String fieldName, String expression) {
    accumulators.add(Accumulators.push(fieldName, expression));
    return this;
}
项目:MongoDB-Plugin    文件:MongoAccumulator.java   
public MongoAccumulator addToSet(String fieldName, String expression) {
    accumulators.add(Accumulators.addToSet(fieldName, expression));
    return this;
}
项目:MongoDB-Plugin    文件:MongoAccumulator.java   
public MongoAccumulator stdDevPop(String fieldName, String expression) {
    accumulators.add(Accumulators.stdDevPop(fieldName, expression));
    return this;
}
项目:MongoDB-Plugin    文件:MongoAccumulator.java   
public MongoAccumulator stdDevSamp(String fieldName, String expression) {
    accumulators.add(Accumulators.stdDevSamp(fieldName, expression));
    return this;
}
项目:ibm-performance-monitor    文件:MongoUtilitiesTest.java   
@Test
public void testFilterParametersBsonDocumentGroup()
{

    Bson group = Aggregates.group("_id", Accumulators.sum("totalQuantity", "$quantity"));
    BsonDocument filterParameters = MongoUtilities.filterParameters(group);

    assertEquals("{ \"$group\" : { \"_id\" : \"_id\", \"totalQuantity\" : { \"$sum\" : \"$quantity\" } } }", filterParameters.toString());

}