title: How to filter array in Mongodb document using Spring
body: <p>I have below document structure.</p>
  
  <pre><code>{
      "_id" : { "teacherId" : "&lt;teacherId&gt;", "Year" : "&lt;Year&gt;" },
      "groups" : [ {
          "groupId" : "&lt;uuid&gt;",
          "groupName" : "&lt;name&gt;",
          "groupNameLowerCase" : "&lt;name_in_lower_case&gt;",
          "description" : "&lt;desc&gt;",
          "students" : ["&lt;studentid1&gt;", "&lt;studentid2&gt;", ...],
          "editedDate" : "&lt;currentTimestamp&gt;"
          },
          ...
      ],
      "editedDate" : "&lt;currentTimestamp&gt;",
      "points" : "&lt;points&gt;"
  }
  </code></pre>
  
  <p>Consider that below two documents are present in DB</p>
  
  <pre><code>{
      "_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"
  </code></pre>
  
  <p>I want to select groups for the specified student and teacher combination. e.g. If I supply teacherid= 1 and student id =11  then query should return two documents with matching groups. I wrote below code to get the matching groups in document. But afterward I understand that elemMatch will only return first element matching. It will return two documents but with only one group in it. </p>
  
  <p>Here I would like to understand the options available in Mongodb 2.4 to filter arrays in document returned by some query. </p>
  
  <pre><code>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&lt;GroupsDocument&gt; groupsDocumentList = groupsMongoTemplate.find(query, GroupsDocument.class);
  </code></pre>