繁体   English   中英

使用MongoDB进行批量API的结果分页

[英]Result pagination for bulk API with MongoDB

MongoDB批量API如何进行结果分页?

API端点(仅针对上下文):

/team/listTeamsForUsers

输入:

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

一个用户可以与多个团队相关联。 因此,API需要基于pageSizepageIndex对结果进行分页的能力。

分页对于单个userId输入是可能的。 我如何支持多个输入的分页?

用例示例:
User01与10个团队相关联。
User02与20个团队相关联。

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.

看到这种实现的例子将是很棒的。

有什么建议么?

假设:

  • 我想一个用户可以是多个团队的成员,而一个团队有多个成员。 因此,用户和团队之间存在多对多的关系。
  • 我进一步假设您有一个连接表,该连接表从userId映射到teamId以便对上述关系进行建模。

表结构:

  • 用户表: id | 名称
  • 球队表: id | 名称
  • UsersTeams: userId | teamId

考虑到您获得了一个userId列表作为输入,用于分页与这些用户关联的团队的SQL代码片段如下所示(请注意,我没有测试以下代码片段)。

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;
  • 传递给limit的参数是pageIndex*pageSize(pageIndex+1)*pagSize
  • 传递的参数inuserIds ,你从端点获得。

尽管此方法易于理解和实施,但它没有最佳性能。 请参阅https://www.xarg.org/2011/10/optimized-pagination-using-mysql/了解MySQL的分页优化(尽管您可能可以将大多数转换为任何SQL数据库)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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