Example usage for com.mongodb.client.model Accumulators sum

List of usage examples for com.mongodb.client.model Accumulators sum

Introduction

In this page you can find the example usage for com.mongodb.client.model Accumulators sum.

Prototype

public static <TExpression> BsonField sum(final String fieldName, final TExpression expression) 

Source Link

Document

Gets a field name for a $group operation representing the sum of the values of the given expression when applied to all members of the group.

Usage

From source file:com.epam.dlab.backendapi.dao.azure.AzureBillingDAO.java

License:Apache License

private Bson groupCriteria() {
    return Aggregates.group(
            getGroupingFields(MongoKeyWords.DLAB_USER, MongoKeyWords.DLAB_ID, MongoKeyWords.RESOURCE_TYPE,
                    MongoKeyWords.METER_CATEGORY, MongoKeyWords.CURRENCY_CODE),
            Accumulators.sum(MongoKeyWords.COST, MongoKeyWords.prepend$(MongoKeyWords.COST)),
            Accumulators.min(MongoKeyWords.USAGE_FROM, MongoKeyWords.prepend$(MongoKeyWords.USAGE_DAY)),
            Accumulators.max(MongoKeyWords.USAGE_TO, MongoKeyWords.prepend$(MongoKeyWords.USAGE_DAY)));
}

From source file:com.epam.dlab.billing.azure.AzureBillingDetailsService.java

License:Apache License

public void updateBillingDetails(String user) {
    log.debug("Updating billing details for user {}", user);

    try {//from   ww  w  .j  a  va 2s  .  c  om
        AggregateIterable<Document> aggregateIterable = mongoDbBillingClient.getDatabase()
                .getCollection(MongoKeyWords.BILLING_DETAILS)
                .aggregate(Lists.newArrayList(
                        Aggregates.match(Filters.and(Filters.eq(MongoKeyWords.DLAB_USER, user),
                                Filters.in(MongoKeyWords.RESOURCE_TYPE, DlabResourceType.EXPLORATORY.toString(),
                                        DlabResourceType.COMPUTATIONAL.toString(),
                                        DlabResourceType.VOLUME.toString()))),

                        Aggregates.group(
                                getGroupingFields(MongoKeyWords.DLAB_ID, MongoKeyWords.DLAB_USER,
                                        MongoKeyWords.EXPLORATORY_ID, MongoKeyWords.RESOURCE_TYPE,
                                        MongoKeyWords.RESOURCE_NAME, MongoKeyWords.COMPUTATIONAL_ID,
                                        MongoKeyWords.METER_CATEGORY),
                                Accumulators.sum(MongoKeyWords.COST,
                                        MongoKeyWords.prepend$(MongoKeyWords.COST)),
                                Accumulators.min(MongoKeyWords.USAGE_FROM,
                                        MongoKeyWords.prepend$(MongoKeyWords.USAGE_DAY)),
                                Accumulators.max(MongoKeyWords.USAGE_TO,
                                        MongoKeyWords.prepend$(MongoKeyWords.USAGE_DAY))),

                        Aggregates.sort(Sorts.ascending(MongoKeyWords.prependId(MongoKeyWords.RESOURCE_NAME),
                                MongoKeyWords.prependId(MongoKeyWords.METER_CATEGORY)))));

        updateBillingDetails(user, mapToDetails(aggregateIterable));
    } catch (RuntimeException e) {
        log.error("Updating billing details for user {} is failed", user, e);
    }
}

From source file:module.script.epilung.SearchSamples.java

License:Open Source License

public SearchSamples() {

    // ===== Connection =====

    MongoClient mongoClient = MongoUtil.buildMongoClient();
    MongoDatabase db = mongoClient.getDatabase("epimed_experiments");
    MongoCollection<Document> collectionSamples = db.getCollection("samples");
    MongoCollection<Document> collectionPlatforms = db.getCollection("platforms");

    Bson filters = Filters.and(/*from w  w w .  ja va2 s.c  o  m*/
            Filters.in("exp_group.id_platform", new String[] { "GPL13534", "GPL8490", "GPL21145" }),
            Filters.eq("exp_group.id_tissue_status", 1), Filters.ne("exp_group.id_topology", null));

    /*
    List<Document> list = collectionSamples
    .find(filters)
    .into(new ArrayList<Document>());
    */

    List<Document> list = collectionSamples.aggregate(Arrays.asList(Aggregates.match(filters),
            Aggregates.group("$exp_group.topology", Accumulators.sum("total", 1)),
            Aggregates.sort(Sorts.orderBy(Sorts.descending("total"))))).into(new ArrayList<Document>());

    for (int i = 0; i < list.size(); i++) {
        System.out.println((i + 1) + " " + list.get(i));
    }

    collectionPlatforms.find(Filters.regex("title", ".*ethyl.*")).forEach(printBlock);

    mongoClient.close();

}

From source file:module.test.SearchPlatforms.java

License:Open Source License

public SearchPlatforms() {

    // ===== Connection =====

    MongoClient mongoClient = MongoUtil.buildMongoClient();
    MongoDatabase db = mongoClient.getDatabase("epimed_experiments");
    MongoCollection<Document> collectionSamples = db.getCollection("sample");
    MongoCollection<Document> collectionPlatforms = db.getCollection("platform");

    List<Document> list = collectionSamples
            .aggregate(Arrays.asList(Aggregates.group("$exp_group.id_platform", Accumulators.sum("total", 1)),
                    Aggregates.sort(Sorts.orderBy(Sorts.descending("total")))))
            .into(new ArrayList<Document>());

    for (int i = 0; i < list.size(); i++) {
        System.out.println((i + 1) + " " + list.get(i));
    }//from  w  w  w . j a  va  2  s.co  m

    // collectionPlatforms.find(Filters.regex("title", ".*ethyl.*")).forEach(printBlock);
    collectionPlatforms.find(Filters.eq("id_organism", "9606")).forEach(printBlock);

    mongoClient.close();

}

From source file:mongodb_teste.Jmongo.java

private void jBProfSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBProfSearchActionPerformed
    // TODO add your handling code here:
    jshowProfSearch.setText("");
    MongoClient mongoClient = new MongoClient();
    MongoDatabase db = mongoClient.getDatabase("BDprject");
    Iterable<Document> iterable = db.getCollection("Professores")
            .aggregate(Arrays.asList((Aggregates.match(new Document("nome", jSearchName.getText()))),
                    Aggregates.unwind("$materias"),
                    Aggregates.group("$materias.nota", Accumulators.sum("total", 1)),
                    Aggregates.sort(new Document("total", -1))));
    Iterator<Document> it = iterable.iterator();
    while (it.hasNext()) {
        jshowProfSearch.append(it.next().toString() + "\n");
    }/*from w  ww.  j  av  a 2s . c  om*/
    mongoClient.close();
}

From source file:mongodb_teste.MongoDB_teste.java

/**
 * @param args the command line arguments
 *//*  w w w .  j a  va  2 s.  c  o  m*/
public static void teste(String[] args) throws ParseException {
    // TODO code application logic here
    MongoClient mongoClient = new MongoClient();
    MongoDatabase db = mongoClient.getDatabase("BDprject");
    DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.ENGLISH);
    /*db.getCollection("Professores").insertOne(
    new Document("nome", "Lana")
        .append("email", "123@ufu.com")
        .append("materias", asList(
                new Document()
                        .append("nome", "EDG")
                        .append("nota", "B"),
                new Document()
                        .append("nome", "BD")
                        .append("nota", "B")))
        .append("telefone", "435341543"));*/
    Iterable<Document> iterable = db.getCollection("Professores")
            .aggregate(Arrays.asList((Aggregates.match(new Document("nome", "Miguel"))),
                    Aggregates.unwind("$materias"),
                    Aggregates.group("$materias.nota", Accumulators.sum("total", 1)),
                    Aggregates.sort(new Document("total", -1))));
    Iterator<Document> it = iterable.iterator();
    while (it.hasNext()) {
        System.out.println(it.next().get("_id"));
    }
}

From source file:org.opencb.cellbase.lib.impl.MongoDBAdaptor.java

License:Apache License

protected QueryResult groupBy(Bson query, String groupByField, String featureIdField, QueryOptions options) {
    if (groupByField == null || groupByField.isEmpty()) {
        return new QueryResult();
    }/*from  ww w  .  jav  a  2 s .  c  o  m*/

    if (groupByField.contains(",")) {
        // call to multiple groupBy if commas are present
        return groupBy(query, Arrays.asList(groupByField.split(",")), featureIdField, options);
    } else {
        Bson match = Aggregates.match(query);
        Bson project = Aggregates.project(Projections.include(groupByField, featureIdField));
        Bson group;
        if (options.getBoolean("count", false)) {
            group = Aggregates.group("$" + groupByField, Accumulators.sum("count", 1));
            return mongoDBCollection.aggregate(Arrays.asList(match, project, group), options);
        } else {
            // Limit the documents passed if count is false
            Bson limit = Aggregates.limit(options.getInt("limit", 10));
            group = Aggregates.group("$" + groupByField,
                    Accumulators.addToSet("features", "$" + featureIdField));
            // TODO change the default "_id" returned by mongodb to id
            return mongoDBCollection.aggregate(Arrays.asList(match, limit, project, group), options);
        }
    }
}

From source file:org.opencb.cellbase.lib.impl.MongoDBAdaptor.java

License:Apache License

protected QueryResult groupBy(Bson query, List<String> groupByField, String featureIdField,
        QueryOptions options) {/*from   www .ja  v a2  s .  c  o  m*/
    if (groupByField == null || groupByField.isEmpty()) {
        return new QueryResult();
    }

    if (groupByField.size() == 1) {
        // if only one field then we call to simple groupBy
        return groupBy(query, groupByField.get(0), featureIdField, options);
    } else {
        Bson match = Aggregates.match(query);
        // add all group-by fields to the projection together with the aggregation field name
        List<String> groupByFields = new ArrayList<>(groupByField);
        groupByFields.add(featureIdField);
        Bson project = Aggregates.project(Projections.include(groupByFields));
        // _id document creation to have the multiple id
        Document id = new Document();
        for (String s : groupByField) {
            id.append(s, "$" + s);
        }
        Bson group;
        if (options.getBoolean("count", false)) {
            group = Aggregates.group(id, Accumulators.sum("count", 1));
            return mongoDBCollection.aggregate(Arrays.asList(match, project, group), options);
        } else {
            // Limit the documents passed if count is false
            Bson limit = Aggregates.limit(options.getInt("limit", 10));
            group = Aggregates.group(id, Accumulators.addToSet("features", "$" + featureIdField));
            // TODO change the default "_id" returned by mongodb to id
            return mongoDBCollection.aggregate(Arrays.asList(match, limit, project, group), options);
        }
    }
}

From source file:org.opencb.cellbase.mongodb.impl.MongoDBAdaptor.java

License:Apache License

protected QueryResult groupBy(Bson query, String groupByField, String featureIdField, QueryOptions options) {
    if (groupByField == null || groupByField.isEmpty()) {
        return new QueryResult();
    }/*ww w . j a v a2  s.c om*/

    if (groupByField.contains(",")) {
        // call to multiple groupBy if commas are present
        return groupBy(query, Arrays.asList(groupByField.split(",")), featureIdField, options);
    } else {
        Bson match = Aggregates.match(query);
        Bson project = Aggregates.project(Projections.include(groupByField, featureIdField));
        Bson group;
        if (options.getBoolean("count", false)) {
            group = Aggregates.group("$" + groupByField, Accumulators.sum("count", 1));
        } else {
            group = Aggregates.group("$" + groupByField,
                    Accumulators.addToSet("features", "$" + featureIdField));
        }
        return mongoDBCollection.aggregate(Arrays.asList(match, project, group), options);
    }
}

From source file:org.opencb.cellbase.mongodb.impl.MongoDBAdaptor.java

License:Apache License

protected QueryResult groupBy(Bson query, List<String> groupByField, String featureIdField,
        QueryOptions options) {// w w w  .  j a v a  2  s  . c  o m
    if (groupByField == null || groupByField.isEmpty()) {
        return new QueryResult();
    }

    if (groupByField.size() == 1) {
        // if only one field then we call to simple groupBy
        return groupBy(query, groupByField.get(0), featureIdField, options);
    } else {
        Bson match = Aggregates.match(query);

        // add all group-by fields to the projection together with the aggregation field name
        List<String> groupByFields = new ArrayList<>(groupByField);
        groupByFields.add(featureIdField);
        Bson project = Aggregates.project(Projections.include(groupByFields));

        // _id document creation to have the multiple id
        Document id = new Document();
        for (String s : groupByField) {
            id.append(s, "$" + s);
        }
        Bson group;
        if (options.getBoolean("count", false)) {
            group = Aggregates.group(id, Accumulators.sum("count", 1));
        } else {
            group = Aggregates.group(id, Accumulators.addToSet("features", "$" + featureIdField));
        }
        return mongoDBCollection.aggregate(Arrays.asList(match, project, group), options);
    }
}