简体   繁体   中英

Pagination with the date for mongodb spring boot

I'm making the chat application. When I'm retrieving the chat history of the user, It is bringing all the records which are present in the database. I just need only the chat history of the last 3 days of that user. Need to apply pagination for this with the date.

I'm sharing my sample code here

This is controller:

@GetMapping("/getAllGroupChatHistory/{userId}")
    ArrayList<ResultGroupChatHistoryDTO> getAllGroupChatHistory(@PathVariable long userId){

    return messagingService.getAllGroupChatHistory(userId);
 }

This is serviceImpl


public ArrayList<ResultGroupChatHistoryDTO> getAllGroupChatHistory(long userId) {

        List<GroupEntity> groupMembers = groupRepository.findAll();

        List<GroupEntity> listgroupMembers = new ArrayList<GroupEntity>();

        groupMembers.forEach(groupMember -> {
            if (groupMember.getParticipates().contains(userId)) {
                listgroupMembers.add(groupMember);
            }
        });
        System.out.println("list" + listgroupMembers);

        List grpIds = listgroupMembers.stream().map(grpEntity -> grpEntity.getGroup_id()).collect(Collectors.toList());

        ArrayList<ResultGroupChatHistoryDTO> finalresult = new ArrayList();

        for (int val = 0; val < grpIds.size(); val++) {

            ResultGroupChatHistoryDTO rchatDTO = new ResultGroupChatHistoryDTO();

            int grpId = (int) grpIds.get(val);

            List<GroupChatEntity> senderHistory = groupChatRepository.findChatByGroupId(grpId);

            ArrayList<GroupChatHistoryDTO> message = new ArrayList();

            for (int i = 0; i < senderHistory.size(); i++) {
                GroupChatHistoryDTO chatDTO = new GroupChatHistoryDTO();
                GroupChatEntity groupChatEntity = senderHistory.get(i);
                chatDTO.setId(groupChatEntity.getSender_id());
                chatDTO.setSenderName(groupChatEntity.getReciever_name());
                chatDTO.setTime(groupChatEntity.getSent_time());
                chatDTO.setLastMessage(groupChatEntity.getMessage());
                message.add(chatDTO);
            }

            rchatDTO.setMessages(message);
            rchatDTO.setId(val);

            finalresult.add(rchatDTO);
        }

        return finalresult;
}

I'm using mongoRepository to connect. How I need to apply the pagination for this based on the date of last 3 days?

You can use Pageable

Pageable pageable = PageRequest.of(0, 20, Sort.by("sent_time").descending())

groupChatRepository.findChatByGroupId(grpId, pageable);
Pageable pageable = PageRequest.of(0, 10);

Query patientsDynamicQuery = new Query().with(pageable);
// Add criteria's according to your wish to Result
List<Patient> filteredPatients = 
mongoTemplate.find(query, Result.class, "patient");
Page<Patient> patientPage = PageableExecutionUtils.getPage(
        filteredPatients,
        pageable,
        () -> mongoTemplate.count(query, Patient.class));

you can use this code because MongoDB doesn't have pagination inbuilt. You can even use OFFSET and LIMIT after sorting the data by date and then return the values by size 10 or more as you like.

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