List of usage examples for com.mongodb.client.model Accumulators addToSet
public static <TExpression> BsonField addToSet(final String fieldName, final TExpression expression)
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 w ww . j a va2s . com*/ 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 ww w . jav a 2s . 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(); }//from www . j ava 2 s. com 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 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)); } else { group = Aggregates.group(id, Accumulators.addToSet("features", "$" + featureIdField)); } return mongoDBCollection.aggregate(Arrays.asList(match, project, group), options); } }
From source file:org.opencb.commons.datastore.mongodb.MongoDBQueryUtils.java
License:Apache License
public static List<Bson> createGroupBy(Bson query, String groupByField, String idField, boolean count) { if (groupByField == null || groupByField.isEmpty()) { return new ArrayList<>(); }//from w ww . j a va 2s . c o m if (groupByField.contains(",")) { // call to multiple createGroupBy if commas are present return createGroupBy(query, Arrays.asList(groupByField.split(",")), idField, count); } else { Bson match = Aggregates.match(query); Bson project = Aggregates.project(Projections.include(groupByField, idField)); Bson group; if (count) { group = Aggregates.group("$" + groupByField, Accumulators.sum("count", 1)); } else { group = Aggregates.group("$" + groupByField, Accumulators.addToSet("features", "$" + idField)); } return Arrays.asList(match, project, group); } }
From source file:org.opencb.commons.datastore.mongodb.MongoDBQueryUtils.java
License:Apache License
public static List<Bson> createGroupBy(Bson query, List<String> groupByField, String idField, boolean count) { if (groupByField == null || groupByField.isEmpty()) { return new ArrayList<>(); }/* w w w. ja v a2 s. c om*/ if (groupByField.size() == 1) { // if only one field then we call to simple createGroupBy return createGroupBy(query, groupByField.get(0), idField, count); } 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(idField); 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 (count) { group = Aggregates.group(id, Accumulators.sum("count", 1)); } else { group = Aggregates.group(id, Accumulators.addToSet("features", "$" + idField)); } return Arrays.asList(match, project, group); } }
From source file:org.opencb.opencga.catalog.db.mongodb.AbstractCatalogMongoDBAdaptor.java
License:Apache License
protected QueryResult groupBy(MongoDBCollection collection, Bson query, String groupByField, String idField, QueryOptions options) {// w w w . j av a 2 s . c om if (groupByField == null || groupByField.isEmpty()) { return new QueryResult(); } if (groupByField.contains(",")) { // call to multiple groupBy if commas are present return groupBy(collection, query, Arrays.asList(groupByField.split(",")), idField, options); } else { Bson match = Aggregates.match(query); Bson project = Aggregates.project(Projections.include(groupByField, idField)); Bson group; if (options.getBoolean("count", false)) { group = Aggregates.group("$" + groupByField, Accumulators.sum("count", 1)); } else { group = Aggregates.group("$" + groupByField, Accumulators.addToSet("features", "$" + idField)); } return collection.aggregate(Arrays.asList(match, project, group), options); } }
From source file:org.opencb.opencga.catalog.db.mongodb.AbstractCatalogMongoDBAdaptor.java
License:Apache License
protected QueryResult groupBy(MongoDBCollection collection, Bson query, List<String> groupByField, String idField, QueryOptions options) { if (groupByField == null || groupByField.isEmpty()) { return new QueryResult(); }/*from www . j a v a 2s . c o m*/ if (groupByField.size() == 1) { // if only one field then we call to simple groupBy return groupBy(collection, query, groupByField.get(0), idField, 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(idField); 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", "$" + idField)); } return collection.aggregate(Arrays.asList(match, project, group), options); } }
From source file:org.opencb.opencga.catalog.db.mongodb.MongoDBAdaptor.java
License:Apache License
protected QueryResult groupBy(MongoDBCollection collection, Bson query, List<String> groupByField, String idField, QueryOptions options) { if (groupByField == null || groupByField.isEmpty()) { return new QueryResult(); }//from www .j a v a2 s .c o m List<String> groupByFields = new ArrayList<>(groupByField); // if (groupByField.size() == 1) { // // if only one field then we call to simple groupBy // return groupBy(collection, query, groupByField.get(0), idField, options); // } else { Bson match = Aggregates.match(query); // add all group-by fields to the projection together with the aggregation field name List<String> includeGroupByFields = new ArrayList<>(groupByField); includeGroupByFields.add(idField); List<Bson> projections = new ArrayList<>(); addDateProjection(projections, includeGroupByFields, groupByFields); projections.add(Projections.include(includeGroupByFields)); Bson project = Aggregates.project(Projections.fields(projections)); // Bson project = Aggregates.project(Projections.include(groupByFields)); // _id document creation to have the multiple id Document id = new Document(); for (String s : groupByFields) { 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", "$" + idField)); } return collection.aggregate(Arrays.asList(match, project, group), options); // } }