com.mongodb.client.model.BsonField Java Examples

The following examples show how to use com.mongodb.client.model.BsonField. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AggregationQuery.java    From immutables with Apache License 2.0 6 votes vote down vote up
private BsonField accumulator(String field, Expression expression) {
  Preconditions.checkArgument(expression instanceof Call, "not a call %s", expression);
  final Call call = (Call) expression;
  final Operator op = call.operator();
  Preconditions.checkArgument(AggregationOperators.isAggregation(op), "not an aggregation operator: %s", op);
  final String name = "$" + naming.get(extractPath(expression));
  if (op == AggregationOperators.AVG) {
    return Accumulators.avg(field, name);
  } else if (op == AggregationOperators.COUNT) {
    return Accumulators.sum(field, 1);
  } else if (op == AggregationOperators.MAX) {
    return Accumulators.max(field, name);
  } else if (op == AggregationOperators.MIN) {
    return Accumulators.min(field, name);
  } else if (op == AggregationOperators.SUM) {
    return Accumulators.sum(field, name);
  } else {
    throw new IllegalArgumentException(String.format("Unknown aggregation operator %s from %s", op, expression));
  }
}
 
Example #2
Source File: MongoReadJournal.java    From ditto with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * For $group stage of an aggregation pipeline over a snapshot collection: take the newest values of fields
 * of serialized snapshots. Always include the first snapshot lifecycle.
 *
 * @param snapshotFields fields of a serialized snapshot to project.
 * @return list of group stage field accumulators.
 */
private List<BsonField> asFirstSnapshotBsonFields(final String... snapshotFields) {
    return Stream.concat(Stream.of(LIFECYCLE), Arrays.stream(snapshotFields))
            .map(fieldName -> {
                final String serializedFieldName = String.format("$%s.%s", SERIALIZED_SNAPSHOT, fieldName);
                return Accumulators.first(fieldName, serializedFieldName);
            })
            .collect(Collectors.toList());
}
 
Example #3
Source File: MongoAccumulator.java    From MongoDB-Plugin with Apache License 2.0 4 votes vote down vote up
public List<BsonField> getAccumulators() {
    return accumulators;
}
 
Example #4
Source File: MongoAccumulator.java    From MongoDB-Plugin with Apache License 2.0 4 votes vote down vote up
public MongoAccumulator setAccumulators(List<BsonField> accumulators) {
    this.accumulators = accumulators;
    return this;
}
 
Example #5
Source File: AggregationPipelineQueryNode.java    From rya with Apache License 2.0 votes vote down vote up
/**
 * 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;
}