gpt4 book ai didi

java - 将 Java Spring 代码从 AggregationOutput 更改为 DBCollection 聚合方法

转载 作者:行者123 更新时间:2023-12-02 10:56:17 25 4
gpt4 key购买 nike

我正在使用最新的库更新 2014 年 Java 项目,MongoDB Java 驱动程序从 3.0 更改为 3.6。大多数代码已更新,但有一个特定的查询非常复杂,这给我带来了问题。

文档是这样的

{
"_id" : ObjectId("58750a67ae28bc28e0705b0f"),
"info": "description",
"parentId", "variable-id-here"
"issues": [
{"name": "a", "closed": true},
{"name": "b", "closed": false},
{"name": "c", "closed": true}
],
"bugs": [
{"name": "d", "closed": false},
{"name": "e", "closed": false},
{"name": "f", "closed": true}
],
"errors": [
{"name": "g", "closed": true},
{"name": "h", "closed": true},
{"name": "i", "closed": false}
]
}

(即使数组中的元素相似,也不能使用带有值 [问题、错误、错误] 的额外“type”键将它们分组到文档中的单个数组中,但这不是重点)

旧脚本就是这个

List<DBObject> aggregators = new ArrayList<>();
DBObject match = new BasicDBObject("$match", new BasicDBObject("parentId", myId));
DBObject project = new BasicDBObject();
List<String> domains = Arrays.asList("issues", "bugs", "errors");

for (Iterator<String> d = domains.iterator(); d.hasNext();) {
String domain = d.next();
//Reset values
aggregators = new ArrayList<>();
// Define MongoDB "$project" to find 'true' values on 'closed' flags
project = new BasicDBObject("$project", new BasicDBObject("closedProblems",
new BasicDBObject("$filter",
new BasicDBObject("input", "$"+domain)
.append("as", "myObject")
.append("cond", new BasicDBObject("$eq",
Arrays.<Object> asList("$$myObject.closed", true)
)
)
)
));

aggregators.add(match);
aggregators.add(project);
//db defined before. AggregationOutput is deprecated so must be changed
AggregationOutput output = db.getCollection("myColl").aggregate(aggregators);

// Now I can iterate results
for (DBObject result : output.results()) {
// ...
}
}

我尝试使用项目、表达式等,但找不到使用新聚合方法复制 MongoDB 项目的方法。

最终结果应该使用mongoTemplate .任何方法执行聚合以加入新的项目指南

最佳答案

在 3.6 驱动程序中使用以下代码。

DBCursor output = (DBCursor) db.getCollection("myColl").aggregate(aggregators, AggregationOptions.builder().build());
for (Iterator<DBObject> r = output.iterator(); output.hasNext();) {
DBObject result = r.next();
...
}

更新:

import static org.springframework.data.mongodb.core.aggregation.Aggregation.*;
import static org.springframework.data.mongodb.core.aggregation.ArrayOperators.Filter.filter;
import static org.springframework.data.mongodb.core.aggregation.ComparisonOperators.Eq.valueOf;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.aggregation.*;

List<AggregationOperation> aggregators;
MatchOperation match = Aggregation.match(Criteria.where("parentId").is(myId));
List<String> domains = Arrays.asList("issues", "bugs", "errors");
for (Iterator<String> d = domains.iterator(); d.hasNext();) {
String domain = d.next();
aggregators = new ArrayList<>();
ProjectionOperation project = project().and(filter(domain)
.as("myObject")
.by(valueOf(
"myObject.closed")
.equalToValue(
true)))
.as("closedProblems");
aggregators.add(match);
aggregators.add(project);
Aggregation aggregation = newAggregation(aggregators).withOptions(newAggregationOptions().cursor(new Document()).build());
AggregationResults<Document> results = mongoTemplate.aggregate(aggregation, "myColl", Document.class );
for (Document result : results) {
// ...
}
}

关于java - 将 Java Spring 代码从 AggregationOutput 更改为 DBCollection 聚合方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51705658/

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