title: Spring data mongoDB : Find all by child object id with Example Executor and Pageable
body: <p>I am facing issue in querying repository with find all by child object ID, Example and Pageable.</p>
  <p>StudentController.java</p>
  <pre><code>@RestController
  @RequestMapping(&quot;/students&quot;)
  public class StudentController {
      @Autowired
      private StudentService studentService;
      
      @RequestMapping(method = RequestMethod.GET)
      public Page&lt;Student&gt; getStudentsPage(
              @PageableDefault(page = 0, size = 10) @SortDefault.SortDefaults({
                      @SortDefault(sort = &quot;id&quot;, direction = Direction.DESC) }) Pageable pageable,
              Student student) {
          return studentService.getStudentPage(student, pageable);
      } 
  }
  </code></pre>
  <p>StudentService.java</p>
  <pre><code>@Service
  public class StudentService {
      private static final Logger logger = LoggerFactory.getLogger(StudentService.class);    
      @Autowired
      private UserService userService;
      
      @Autowired
      private StudentRepository studentRepository;
      
      public Page&lt;Student&gt; getStudentsPage(Student student, Pageable pageable) {
          logger.debug(&quot;Getting Students : {}, {}&quot;, student, pageable);
          //gets organization Id of the student from the session
          String organizationId = userService.getUserMe().getOrganization().getId(); 
          Page&lt;Student&gt; studentPage = studentRepository.findAllByOrganizationId(organizationId, Example.of(student), pageable);
          
          logger.debug(&quot;Students: {}&quot;, studentPage.getContent());
          return studentPage;
      }
  }
  </code></pre>
  <p>StudentRepository.java</p>
  <pre><code>@Repository
  public interface StudentRepository extends MongoRepository&lt;Student, String&gt; {    
      Page&lt;Student&gt; findAllByOrganizationId(String organizationId, Example&lt;Student&gt; example, Pageable pageable);
      
  }
  </code></pre>
  <p>Student.java</p>
  <pre><code>@Document(collection = &quot;students&quot;)
  public class Student {
      @Id
      private String       id;
      private String       firstName;
      private String       lastName;
      @DBRef(db = &quot;organizations&quot;)
      private Organization organization;
      
      public String getId() {
          return id;
      }
      
      public void setId(String id) {
          this.id = id;
      }
      
      public String getFirstName() {
          return firstName;
      }
      
      public void setFirstName(String firstName) {
          this.firstName = firstName;
      }
      
      public String getLastName() {
          return lastName;
      }
      
      public void setLastName(String lastName) {
          this.lastName = lastName;
      }
      
      public Organization getOrganization() {
          return organization;
      }
      
      public void setOrganization(Organization organization) {
          this.organization = organization;
      }
      
      @Override
      public String toString() {
          return &quot;Student [id=&quot; + id + &quot;, firstName=&quot; + firstName + &quot;, lastName=&quot; + lastName + &quot;, organization=&quot;
                 + organization + &quot;]&quot;;
      }
      
  }
  </code></pre>
  <p>Organization.java</p>
  <pre><code>@Document(collection = &quot;organizations&quot;)
  public class Organization {
      @Id
      private String id;
      private String name;
      
      public String getId() {
          return id;
      }
      
      public void setId(String id) {
          this.id = id;
      }
      
      public String getName() {
          return name;
      }
      
      public void setName(String name) {
          this.name = name;
      }
      
      @Override
      public String toString() {
          return &quot;Organization [id=&quot; + id + &quot;, name=&quot; + name + &quot;]&quot;;
      }
      
  }
  </code></pre>
  <p>The GET API request:
  http://localhost:8080/students?page=0&amp;size=10&amp;sort=id,desc&amp;lastName=xyz</p>
  <p>the repository method
  Page findAllByOrganizationId(String organizationId, Example example, Pageable pageable);
  should return all the records with matching organizationId and lastName=xyz, But it returns the records that match with the organization Id along with all the students irrespective of the lastName.</p>
  <p>Is something wrong with the code?</p>