gpt4 book ai didi

java - 使用 Java 对 MongoDB 中的数组进行排序并选择字段

转载 作者:太空宇宙 更新时间:2023-11-04 10:44:42 25 4
gpt4 key购买 nike

我正在尝试使用 MongoDB 聚合转换以下数据。我想要对 array 'connections' 进行排序,并且只想要名称与我的正则表达式匹配的数组元素。

在这种情况下,我希望数组按“步数”(中间 friend 的数量)排序,并且只排序那些名字中带有“Han”的人。在本例中,这将导致“Han Solo”。

sortmatch2 操作都没有达到我预期的效果。数组未排序,根本不匹配...我做错了什么?欢迎对此提供任何其他反馈,这是我第一次在 Java 中使用 MongoDB

谢谢!

{
"name": "Luke Skywalker",
"_id": 1,
"connections": [
{
"name": "Tendra Risant",
"_id": 5,
"steps": 2
},
{
"name": "Han Solo",
"_id": 2,
"steps": 0
},
{
"name": "Leia Organa",
"_id": 3,
"steps": 0
},
{
"name": "Luke Skywalker",
"_id": 1,
"steps": 1
},
{
"name": "Lando Clarissian",
"_id": 4,
"steps": 1
}
]
}


public List<DBObject> search(final int id, final String value) {
AggregationOperation graphlookup = new AggregationOperation() {
@Override
public DBObject toDBObject(AggregationOperationContext aggregationOperationContext) {
DBObject graphLookup = new BasicDBObject("from", "people")
.append("startWith", "$friends") //start at friends array
.append("connectFromField", "friends") //Links a value from the array friends to the ...
.append("connectToField", "_id") // ... id of a following document -> creating a chain of friends
.append("maxDepth",3)
.append("depthField","steps")
.append("as", "connections");
return new BasicDBObject("$graphLookup", graphLookup);
}
};

AggregationOperation project = new AggregationOperation() {
@Override
public DBObject toDBObject(AggregationOperationContext aggregationOperationContext) {
DBObject match = new BasicDBObject("connections.name", 1)
.append("connections._id", 1)
.append("connections.steps", 1)
.append("name", 1);
return new BasicDBObject("$project", match);
}
};

AggregationOperation match = Aggregation.match(Criteria.where("_id").is(id));
AggregationOperation sort = Aggregation.sort(Sort.Direction.ASC, "connections.steps");
AggregationOperation match2 = Aggregation.match(Criteria.where("connections.name").regex(".*Han.*"));

Aggregation aggregation = Aggregation.newAggregation(graphlookup, project, sort, match, match2);
List<DBObject> output = mongoTemplate.aggregate(aggregation, "people", DBObject.class).getMappedResults();
return output;
}

最佳答案

您无法就地对数组进行排序。所以你需要$unwind + $sort/$match + $group

将最后几行代码更改为

 AggregationOperation match = Aggregation.match(Criteria.where("_id").is(1));
AggregationOperation unwind = Aggregation.unwind("connections");
AggregationOperation match2 = Aggregation.match(Criteria.where("connections.name").regex(".*Han.*"));
AggregationOperation sort = Aggregation.sort(Sort.Direction.ASC, "connections.steps");
AggregationOperation group = Aggregation.group("_id").push("connections").as("connections").first("name").as("name");
AggregationOperation project2 = Aggregation.project("connections").andExclude("_id").andInclude(Fields.from(Fields.field("name", "id")));

关于java - 使用 Java 对 MongoDB 中的数组进行排序并选择字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48546819/

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