对数组进行排序,并使用Java在MongoDB中选择字段

时间:2018-01-31 16:22:47

标签: java arrays mongodb sorting aggregate

我正在尝试使用MongoDB聚合转换以下数据。我希望array'连接'排序,我只想要名称与我的正则表达式匹配的数组元素。

在这种情况下,我希望array按“步骤”(中间朋友的数量)排序,只有名字中包含“汉”的人。在这个例子中,这将导致'Han Solo'。

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

谢谢!

{
"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;
}

1 个答案:

答案 0 :(得分:0)

您无法对阵列进行排序。所以你需要$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")));