engineD
(David Glenn)
April 19, 2020, 12:06am
1
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?
engineD
(David Glenn)
April 23, 2020, 10:26pm
2
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: Fast and Efficient Pagination in MongoDB | Codementor
Hope it helps.