【问题标题】:MongoDB Stitch iOS: Pagination with skip in collection.find()MongoDB Stitch iOS:在collection.find()中跳过分页
【发布时间】:2020-08-25 04:17:12
【问题描述】:

我正在尝试在 iOS 版 Swift 中使用 MongoDB Stitch 实现分页,但我在 docs 中看不到 skip

RemoteFindOptions 有排序和限制,但没有像这样的跳过:

{
   "projection": <document>,
   "sort": <document>,
   "limit": <integer>
}

有没有办法用 iOS 的 mongodb 缝合来实现 skip

【问题讨论】:

    标签: ios swift mongodb mongodb-stitch


    【解决方案1】:

    有没有办法在 iOS 上使用 mongodb 缝合来实现跳过?

    对于分页,您可以创建一个Stitch Function,使用limit 返回有限数量的结果。

    基本上执行类似于以下的操作:

    db.collection.find({ "_id": 100 })
        .sort({ _id: 1 })
        .limit(50)
    

    上面的示例 sn -p 显示了一个示例分页为 50。在客户端,请务必记录最后一个_id,以便您可以执行另一个分页请求。即

    db.collection.find({ "_id": 150 })
        .sort({ _id: 1 })
        .limit(50)
    

    另见:

    【讨论】:

      【解决方案2】:

      您可以使用aggregate 代替find。例如:

      myCollection.aggregate([
          { $sort: { createdAt: -1 } },
          { $skip: 10 },
          { $limit: 10 }
      ]).toArray()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-08-08
        • 2019-08-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-29
        • 1970-01-01
        相关资源
        最近更新 更多