简体   繁体   中英

Result pagination for bulk API with MongoDB

How does results pagination work for bulk API with MongoDB?

API endpoint(just for context):

/team/listTeamsForUsers

Input:

{
   "userIds": ["userId1", "userId2", "userId3"...],
   "options": {
       "pageSize": 10,
       "pageIndex": 0
    }
}

A user can be associated with multiple teams. Hence the API needs ability to paginate the results, based on pageSize and pageIndex .

Pagination is possible for single userId input. How do I support pagination for multiple inputs?

Example use case:
User01 is associated to 10 teams.
User02 is associated to 20 teams.

when pageSize=10 and pageIndex=0
    Teams 1-10 related to User01 should be returned.

when pageSize=10 and pageIndex=1
    Teams 1-10 related to User02 should be returned.

when pageSize=10 and pageIndex=2
    Teams 11-20 related to User02 should be returned.

It would be great to see examples of such implementation.

Any suggestions?

Assumptions:

  • I suppose a user can be a member in multiple teams and a team has multiple members. Therefore, users and teams are in a many to many relationship.
  • I further assume you have a junction table that maps from userId to teamId in order to model the above relationship.

Table structures:

  • Users table : id | name
  • Teams table : id | name
  • UsersTeams : userId | teamId

Considering you get a list of userId as input, a SQL snippet for paging the teams associated with those users would look as follows (please note I did not test the below snippet).

select distinct t.name
    from team t, user u, userTeam ut
    where t.id = ut.teamId and u.id = ut.userId and u.id in (1, 2)
    order by t.name desc
    limit 0, 10;
  • The parameters passed to limit are the pageIndex*pageSize and (pageIndex+1)*pagSize .
  • The parameters passed to in are the userIds you get from the endpoint.

Although this approach is easy to understand and implement, it does not have the best performance. Please see https://www.xarg.org/2011/10/optimized-pagination-using-mysql/ for paging optimizations for MySQL (although you probably can translate most of that to any SQL database).

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