简体   繁体   中英

Spring data - MongoDB : How to return a page/slice of only specific fields

I want to implement pagination with scroll. My goal is to get a slice of data when scroll event is triggered.

My document "Foo" structure is relatively complex with a lot of nested objects. But I need to show only two fields from these documents.

I have two questions:

  • In general, is specifying the fields in the query will enhance the performance? (seing that we dont get the whole document)

for example:

    @Query(value="{ path : ?0}", fields="{ path : 0 }")
    List<Foo> findByPath(String path);  
  • If yes, How can I get a slice of documents with specific fields using "Pagebale"

      Slice<Foo> findSliceBy(Pageable pageable); 

When specifying the fields the performance is enhanced.

Just annotating the findSliceBy method with @Query will do the job

    @Query(fields="{path:1, name:1}")
   Slice<Foo> findSliceBy(Pageable pageable);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM