簡體   English   中英

帶有mod的java中的MongoDb聚合查詢

[英]MongoDb aggregate query in java with mod

你好,我在我的 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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM