2016-11-16 36 views
0

我有下面的文檔結構。如何使用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

正如你所說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(); 
+0

你有什麼例子吧 – Sachin

+0

我已經更新了代碼 –

相關問題