I can't check this code because I don't have the collection, but it should be something like this:
BasicDBList modArgs = new BasicDBList();
modArgs.add("$total");
modArgs.add("$_id");
coll.aggregate(asList(
group("$groupValue", sum("total", "$weight")),
project(fields(computed("groupValue", "$_id"),
include("total"), excludeId(),
computed("remainder", new BasicDBObject("$mod", modArgs))))
));
Note that I'm using a bunch of static imports:
import static com.mongodb.client.model.Accumulators.sum;
import static com.mongodb.client.model.Aggregates.group;
import static com.mongodb.client.model.Aggregates.project;
import static com.mongodb.client.model.Projections.computed;
import static com.mongodb.client.model.Projections.excludeId;
import static com.mongodb.client.model.Projections.fields;
import static com.mongodb.client.model.Projections.include;