下面列出了怎么用com.mongodb.client.model.BsonField的API类实例代码及写法,或者点击链接到github查看源代码。
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));
}
}
/**
* 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());
}
/**
* 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;
}
public List<BsonField> getAccumulators() {
return accumulators;
}
public MongoAccumulator setAccumulators(List<BsonField> accumulators) {
this.accumulators = accumulators;
return this;
}