question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

skip and limit is not returning the right document (typescript)

See original GitHub issue

Do you want to request a feature or report a bug? BUG

What is the current behavior? skip and limit is returning wrong documents, they are not what is expected

If the current behavior is a bug, please provide the steps to reproduce.

sandbox link

tsconfig

What is the expected behavior? The expected behavior is, the skip should skip the document and show the right result,

How to reproduce the issue

  1. Go to https://xxxxx/api/v1/trips?fields=name,ratingsAverage&paginate=1&limit=10 Above will return 9 docs. This is maximum number of docs I have.

  2. Now use https://xxxxx/api/v1/trips?fields=name,ratingsAverage&paginate=1&limit=2 (1 st page) This will return 2 docs. Observe the name property.

  3. Now use https://xxxxx/api/v1/trips?fields=name,ratingsAverage&paginate=2&limit=2( 2 nd page) This will return 2 docs. Observe the name property.

Try it for page 3 also.

You will notice that the trip name Forest Hiker or The Sea explorer or some name is repeating unnecessary. So I am sometimes getting the same trip name. The trips name should not repeat, until, I start it from page 1 again. Either the skip is not working properly, or there is bug in my code. I tried with raw mongo query, skip and limit works fine.

Pls take a look at tripsController.ts apiFeatures.ts

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that “latest” is not a version. MongoDB 4,4 Mongoose 5.13.0 Node js 14.16.1

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
vkarpov15commented, Aug 10, 2021

I took a closer look and I can confirm this is expected behavior. Even with the MongoDB shell I get the ordering issue:

MongoDB Enterprise > db.trips.find({ secretTrip: { $ne: true } }, { name: 1 }).sort({ createdAt: -1 }).skip(2).limit(2)
{ "_id" : ObjectId("5c88fa8cf4afda39709c2955"), "name" : "The Sea Explorer" }
{ "_id" : ObjectId("5c88fa8cf4afda39709c295a"), "name" : "The Snow Adventurer" }
MongoDB Enterprise > db.trips.find({ secretTrip: { $ne: true } }, { name: 1 }).sort({ createdAt: -1 }).skip(0).limit(2)
{ "_id" : ObjectId("5c88fa8cf4afda39709c2955"), "name" : "The Sea Explorer" }
{ "_id" : ObjectId("5c88fa8cf4afda39709c2951"), "name" : "The Forest Hiker" }
MongoDB Enterprise > 

There’s no guarantee as to how the MongoDB server breaks ties when sorting. In addition to fixing your schema definition’s default createdAt, you should also add _id to your sort() if you have a lot of ties and want to break them:

const trips = await Trips.find().sort({ createdAt: -1, _id: -1 }).skip(3).limit(1);
0reactions
DVGYcommented, Jul 3, 2021

@vkarpov15 . @IslandRhythms

  1. To be honest it does not even for the property ratingsAverage. I have two tours who ratingsAverage is 4.8 and it fails there.
  2. This works fine in compass. I checked If I use raw mongo db query I get the right results

Update: @vkarpov15 I got from your above comment and here. Thanks

Read more comments on GitHub >

github_iconTop Results From Across the Web

how to set limit,offset,skip in mongoose query - Stack Overflow
I am using express,mongoose,node.so i have checked this query regarding to skip and limit of documents ...
Read more >
Single fetch with skip and limit returns unexpected result
I have MongoDB collection that have 5 documents, which I am running a query against it. The query fetches one document at a...
Read more >
Documentation - Narrowing - TypeScript
Understand how TypeScript uses JavaScript knowledge to reduce the amount of type syntax in your projects.
Read more >
Fast and Efficient Pagination in MongoDB | Codementor
skip (n) will skip n documents from the cursor while limit(n) will cap the number of documents to be returned from the cursor....
Read more >
Query Cheat Sheet - GROQ - Sanity.io
Everything, i.e. all documents *[] // Everything with no filters applied, ... asc) // limit/offset using external params (see client documentation) *[_type ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found