MongoDB.live, free & fully virtual. June 9th - 10th. Register Now

Stitch pagination

I’m building an API using Stitch. Using a simple GET request and obtaining a set of documents from the DB. See code below:

const docs = context.services.get("mongodb-atlas")
      .db("bc-notes-db")
      .collection("notes")
      .find({})
      .sort({shortnote:1})
      .skip(1)
      .limit(9);

I get an error using the line

.skip(1)

stating that

"error": "TypeError: 'skip' is not a function",

Any ideas how I can implement pagination in this situation? or why this doesn’t work?

In order to use “skip” needed to use aggregate instead of find? see below…

const docs = context.services.get("mongodb-atlas")
      .db("bc-notes-db")
      .collection("notes")
      .aggregate([
        {"$skip": 1},
        {"$limit": 9}
        ])
      .toArray();

Hi,
In my code I don’t use skip, because I read somewhere it is inefficient. Instead you can use find by _id and use limit. The second approach described in this article: https://www.codementor.io/@arpitbhayani/fast-and-efficient-pagination-in-mongodb-9095flbqr

Hope it helps.

Here is the performance test: https://arpitbhayani.me/blogs/benchmark-and-compare-pagination-approach-in-mongodb