如何使用Spring过滤Mongodb文档中的数组

时间:2016-11-16 06:37:54

标签: mongodb spring-data spring-mongodb

我的文档结构如下。

{
    "_id" : { "teacherId" : "<teacherId>", "Year" : "<Year>" },
    "groups" : [ {
        "groupId" : "<uuid>",
        "groupName" : "<name>",
        "groupNameLowerCase" : "<name_in_lower_case>",
        "description" : "<desc>",
        "students" : ["<studentid1>", "<studentid2>", ...],
        "editedDate" : "<currentTimestamp>"
        },
        ...
    ],
    "editedDate" : "<currentTimestamp>",
    "points" : "<points>"
}

考虑下面两个文档存在于DB

{
    "_id" : { "teacherId" : "1", "Year" : "2016" },
    "groups" : [ {
        "groupId" : "123",
        "groupName" : "Test1",
        "groupNameLowerCase" : "test1",
        "description" : "sample document",
        "students" : ["11", "22"]
         },
    {
        "groupId" : "234",
        "groupName" : "Test2",
        "groupNameLowerCase" : "test2",
        "description" : "sample document",
        "students" : ["11", "22"]
         },
        {
        "groupId" : "345",
        "groupName" : "Test3",
        "groupNameLowerCase" : "test3",
        "description" : "sample document",
        "students" : ["21", "32"]
         }
    ],
    "points" : "650"
}

{
    "_id" : { "teacherId" : "1", "Year" : "2015" },
    "groups" : [ {
        "groupId" : "123",
        "groupName" : "HOCKEY",
        "groupNameLowerCase" : "HOCKEY",
        "description" : "HOCKEY team",
        "students" : ["11", "22"]
         },
        {
        "groupId" : "234",
        "groupName" : "football",
        "groupNameLowerCase" : "football",
        "description" : "sample football",
        "students" : ["11", "22"]
         },
        {
        "groupId" : "345",
        "groupName" : "Test3",
        "groupNameLowerCase" : "test3",
        "description" : "sample document",
        "students" : ["21", "32"]
         }
    ],
    "points" : "650"

我想为指定的学生和教师组合选择小组。例如如果我提供teacherid = 1且student id = 11,则查询应返回两个具有匹配组的文档。我在下面写了代码来获取文档中的匹配组。但后来我明白elemMatch只返回第一个元素匹配。它将返回两个文档,但只包含一个组。

在这里,我想了解Mongodb 2.4中可用的选项,以过滤某些查询返回的文档中的数组。

String teacherId = "1";
String studentId = "11";

Criteria documentSearchCriteria = where("_id.teacherId").is(teacherId)
                .and("groups")
                .elemMatch(where("students").in(studentId));

Criteria groupFilterCriteria = where("groups").elemMatch(where("students").in(studentBid));
BasicQuery query = new BasicQuery(documentSearchCriteria.getCriteriaObject(), groupFilterCriteria.getCriteriaObject());
List<GroupsDocument> groupsDocumentList = groupsMongoTemplate.find(query, GroupsDocument.class);

1 个答案:

答案 0 :(得分:1)

正如您所说,elemMatch将仅检索数组中的第一个对象,因此您必须使用聚合未来来实现输出

    MatchOperation match = Aggregation.match(Criteria.where("_id.teacherId").is("1").and("groups.students").in(11));
    UnwindOperation unwind = Aggregation.unwind("groups");
    GroupOperation group = Aggregation.group("_id").push("groups").as("groups").first("points").as("points");
    Aggregation aggregation = Aggregation.newAggregation(unwind, match, group);
    AggregationResults<BasicDBObject> groupResults = mongoTemplate.aggregate(aggregation,
                    CustomGroupsDocument.class, BasicDBObject.class);
    List<BasicDBObject> result = groupResults.getMappedResults();