您好,我在shell中运行以下命令:
db.getCollection('unaProva').aggregate( [ {
$group : { _id : "$groupValue" ,total:{$sum:"$weight"}} }, {
$project : { _id:0,groupValue : "$_id" , total : "$total" ,
remainder: { $mod: [ "$total", "$_id" ] } } }] )
我可以工作,但是我需要它可以在我的Java代码中工作,而且我不知道如何转换它。
我无法检查此代码,因为我没有集合,但是它应该是这样的:
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;
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句