gpt4 book ai didi

mongodb - 将 MongoDB 查询转换为 MongoDB java 驱动程序查询

转载 作者:可可西里 更新时间:2023-11-01 09:59:18 24 4
gpt4 key购买 nike

请帮助我使用 Java MongoDB 驱动程序将以下 MongoDB 查询转换为基于 Java 的查询。谢谢。

db.playerscorecollection.aggregate(
{ $unwind: "$scorearray"},
{ $group: { _id: { player: "$player", venue: "$scorearray.venue", score: "$scorearray.score" } } },
{ $sort: { "_id.score" : 1 } },
{ $group: { _id: "$_id.player", maxScore: { $last: "$_id.score" }, venue: { $last: "$_id.venue"} } }
)

最佳答案

我还没有检查语法。另外,我不知道您的查询是否有效,但这是我的尝试。

        //unwind
DBObject unwind = new BasicDBObject("$unwind", "$scorearray");
// Now the $group operation
DBObject groupFields = new BasicDBObject("player", "$player");
groupFields.put("venue", "$scorearray.venue"));
groupFields.put("score", "$scorearray.score"));
DBObject group = new BasicDBObject("$group", new BasicDBObject("_id", groupFields));
//sort
DBObject sort = new BasicDBObject("$sort", new BasicDBObject("_id.score":1));
//secondgroup
DBObject secondGroupFields = new BasicDBObject("_id", "$_id.player")
secondGroupFields.put("maxScore", new BasicDBObject("$last":"$_id.score"));
secondGroupFields.put("venue", new BasicDBObject("$last":"$_id.venue"));
DBObject secondGroup = new BasicDBObject("$group", secondGroupFields);

// run aggregation
AggregationOutput output = playerScoreCollection.aggregate(unwind, group,sort,secondGroup);

Iterable<DBObject> result = output.results();
Iterator<DBObject> iterator = result.iterator();

关于mongodb - 将 MongoDB 查询转换为 MongoDB java 驱动程序查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17628786/

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