您好我在我的shell中运行此命令:
db.getCollection('unaProva').aggregate( [ {
$group : { _id : "$groupValue" ,total:{$sum:"$weight"}} }, {
$project : { _id:0,groupValue : "$_id" , total : "$total" ,
remainder: { $mod: [ "$total", "$_id" ] } } }] )
我的工作,但我需要它在我的Java代码中工作,我不知道如何转换它。
答案 0 :(得分:1)
我无法检查此代码,因为我没有该集合,但它应该是这样的:
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))))
));
请注意,我正在使用一堆静态导入:
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;