In daily business, you may encounter deduplication based on a certain field in the database. We can use the mongotemplate.findDistinct method. However, using this method does not match well with other business requirements such as sorting, pagination, and getting the total count. To better implement the business logic, we can use MongoDB's pipeline aggregation. Below is the code:
Condition + Deduplication + Sorting + Pagination
Entity Class
Database Data
Execution Result