gpt4 book ai didi

mongodb - 如何在Mongodb java驱动程序中编写多个group by id字段

转载 作者:IT老高 更新时间:2023-10-28 13:35:36 28 4
gpt4 key购买 nike

在下面的查询中

{ $group : {
_id : { success:'$success', responseCode:'$responseCode', label:'$label'},
max_timeStamp : { $timeStamp : 1 },
count_responseCode : { $sum : 1 },
avg_value : { $sum : "$value" },
count_success : { $sum : 1 }
}}

_id : {success:'$success', responseCode:'$responseCode', label:'$label'},如何翻译成在java mongodb驱动中使用。

我试过了

BasicDBList list = new BasicDBList();
list.add(new BasicDBObject("success", "$success"));
list.add(new BasicDBObject("responseCode", "$responseCode"));
list.add(new BasicDBObject("label", "$label"));
AggregationOutput output = collection.aggregate(match, project, group);

多维数组

String [][] muitiGroupBy = {{"success", "$success"},{"responseCode", "$responseCode"},{"label", "$label"}};

等等。

但我总是得到这样的结果

"_id" : [ { "success" : "$success"} , { "responseCode" : "$responseCode"}]

如果我只使用一个字段,它会起作用。

DBObject groupFields = new BasicDBObject("_id", new BasicDBObject("success", "$success"));

最佳答案

我们确实想出了办法。我们可以使用 this 来实现.

Map<String, Object> dbObjIdMap = new HashMap<String, Object>();
dbObjIdMap.put("success", "$success");
dbObjIdMap.put("responseCode", "$responseCode");
dbObjIdMap.put("label", "$label");
DBObject groupFields = new BasicDBObject( "_id", new BasicDBObject(dbObjIdMap));

关于mongodb - 如何在Mongodb java驱动程序中编写多个group by id字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15564562/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com