MongoDb Java using sum() in Aggregations with literal value (not with reference)

34 views Asked by At

This Java statement

Aggregation.group("name").sum("1").as("count")

generates

{"$group" : { "_id": "$name", "count": { "$sum": "$1" } } }

I'd like to specify the $sum value without a reference (removing the $), so it becomes

{ "$sum": "1" }

How to do that ? Thanks.

1

There are 1 answers

0
Martin On

With my original question, the aim was to use the aggregations via MongoTemplate from Spring, but I found a way to use it with MongoClient manually specifying the fields like this :

Bson group = new Document("$group", new Document("_id", "$name").append("count", new Document("$sum", 1)));
Bson match = new Document("$match", new Document("count", new Document("$gt", 1)));

AggregateIterable<Document> documents = collection.aggregate(List.of(group, match));