private static <TExpression> BsonField accumulator(final String name, final String fieldName, final TExpression expression) { return new BsonField(fieldName, new SimpleExpression<TExpression>(name, expression)); }
/** * Get the group stage accumulator fields and expressions. * @param projection Projection * @return Accumulator fields and expression */ private static List<BsonField> getGroupAccumulators(BsonProjection<?> projection) { final List<BsonField> fieldAccumulators = new LinkedList<>(); if (projection != null) { for (Entry<String, Bson> field : projection.getFields().entrySet()) { if (field.getValue() != null) { fieldAccumulators.add(new BsonField(field.getKey(), field.getValue())); } else { fieldAccumulators.add(new BsonField(field.getKey(), new Document("$first", "$" + field.getKey()))); } } } return fieldAccumulators; }
/** * Add a $group step to filter out redundant solutions. * @return True if the distinct operation was successfully appended. */ public boolean distinct() { List<String> key = new LinkedList<>(); for (String varName : bindingNames) { key.add(hashFieldExpr(varName)); } List<BsonField> reduceOps = new LinkedList<>(); for (String field : FIELDS) { reduceOps.add(new BsonField(field, new Document("$first", "$" + field))); } pipeline.add(Aggregates.group(new Document("$concat", key), reduceOps)); return true; }
/** * Add a $group step to filter out redundant solutions. * @return True if the distinct operation was successfully appended. */ public boolean distinct() { final List<String> key = new LinkedList<>(); for (final String varName : bindingNames) { key.add(hashFieldExpr(varName)); } final List<BsonField> reduceOps = new LinkedList<>(); for (final String field : FIELDS) { reduceOps.add(new BsonField(field, new Document("$first", "$" + field))); } pipeline.add(Aggregates.group(new Document("$concat", key), reduceOps)); return true; }
MongoCollection<Document> dbCollection = db.getCollection("example", Document.class); AggregateIterable<org.bson.Document> aggregate = dbCollection.aggregate(Arrays.asList(Aggregates.group("_id", new BsonField("averageAge", new BsonDocument("$avg", new BsonString("$age")))))); Document result = aggregate.first(); double age = result.getDouble("averageAge");
private static Bson createGroupStage() { return group(new BsonDocument(FIELD_ID, new BsonString(ID_VARIABLE)), new BsonField(FIELD_NAMESPACE, createProjectionDocument(FIRST_PROJECTION, NAMESPACE_VARIABLE)), new BsonField(FIELD_ATTRIBUTES, createProjectionDocument(FIRST_PROJECTION, FIELD_ATTRIBUTES_VARIABLE)), new BsonField(FIELD_FEATURES, createProjectionDocument(FIRST_PROJECTION, FIELD_FEATURES_VARIABLE)), new BsonField(FIELD_INTERNAL, createProjectionDocument(PUSH_PROJECTION, FIELD_INTERNAL_VARIABLE)), new BsonField(FIELD_DELETED, createProjectionDocument(FIRST_PROJECTION, FIELD_DELETED_VARIABLE)), new BsonField(FIELD_DELETED_FLAG, createProjectionDocument(FIRST_PROJECTION, FIELD_DELETED_FLAG_VARIABLE)), new BsonField(FIELD_REVISION, createProjectionDocument(FIRST_PROJECTION, FIELD_REVISION_VARIABLE))); }
private static Bson createGroupStage() { return group(new BsonDocument(FIELD_ID, new BsonString(ID_VARIABLE)), new BsonField(FIELD_NAMESPACE, createProjectionDocument(FIRST_PROJECTION, NAMESPACE_VARIABLE)), new BsonField(FIELD_ATTRIBUTES, createProjectionDocument(FIRST_PROJECTION, FIELD_ATTRIBUTES_VARIABLE)), new BsonField(FIELD_FEATURES, createProjectionDocument(FIRST_PROJECTION, FIELD_FEATURES_VARIABLE)), new BsonField(FIELD_INTERNAL, createProjectionDocument(PUSH_PROJECTION, FIELD_INTERNAL_VARIABLE)), new BsonField(FIELD_DELETED, createProjectionDocument(FIRST_PROJECTION, FIELD_DELETED_VARIABLE)), new BsonField(FIELD_REVISION, createProjectionDocument(FIRST_PROJECTION, FIELD_REVISION_VARIABLE))); }
private static <TExpression> BsonField accumulator(final String name, final String fieldName, final TExpression expression) { return new BsonField(fieldName, new SimpleExpression<TExpression>(name, expression)); }
private static void addCountStage(final Collection<Bson> pipeline, final boolean isCount) { if (isCount) { pipeline.add(group(new BsonDocument(FIELD_ID, BsonNull.VALUE), new BsonField(COUNT_RESULT_NAME, new BsonDocument(SUM_GROUPING, BSON_INT_1)))); } }
private static void addCountStage(final Collection<Bson> pipeline, final boolean isCount) { if (isCount) { pipeline.add(group(new BsonDocument(FIELD_ID, BsonNull.VALUE), new BsonField(COUNT_RESULT_NAME, new BsonDocument(SUM_GROUPING, BSON_INT_1)))); } }