Search code examples
mongodbspring-data-mongodb

Spring Data Mongo - bring paginated data without embedded document


I went through many links like Spring Data Mongo: How to return nested object by its field?. I am using Spring Boot v2.2.2.RELEASE + Spring Data Mongo example.

I want to get all employees with Paginated data, but I don't want to bring the Departments.

What would I need to change in Page<Employee> findAll(Pageable pageable)?

{
    "_id" : ObjectId("5e4143762d8c210ff48f1026"),
    "firstName" : "John",
    "lastName" : "Doe",
    .....
    ......
    ......
    .......
    "departments" : [ 
        {
            "departmentName" : "IT Department",
            "departmentCode" : "IT",
            ....
            ....
        }, 
         ....
         ....
    ]
}

Solution

  • I was able to do it using below Query of Spring Data Mongo.

    @Query(value = "{}", fields = "{'departments': 0}")
    Page<Employee> findAll(Pageable pageable);