gpbaculio
gpbaculio

Reputation: 5968

query first number of items after an id in mongoose?

i am thinking of implementing a cursor based pagination and I think of encoding and decoding an id. is it possible to query like this in mongoose?

MyModel.find({ _id: 4 }).limit(10)

so basically after that id, you query for the next 10 items?

Upvotes: 3

Views: 399

Answers (1)

Harshal Yeole
Harshal Yeole

Reputation: 4983

You can easily do it using

skip & limit

For Eg:

MyModel.find({ _id: 4 }).skip(0).limit(10)

MyModel.find({ _id: 4 }).skip(10).limit(10)

You can keep increasing the skip value by 10 and your pagination is done!

Read more: https://www.w3resource.com/mongodb/mongodb-skip-limit.php

Upvotes: 3

Related Questions