Skip to content

Sorting with equality does not ensure order in pagination #14718

@arenier

Description

@arenier

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

8.3.2

Node.js version

18.20.2

MongoDB server version

6.X

Typescript version (if applicable)

No response

Description

My CourseModel contains fields as title and code and other fields.

I have 3 courses with the same title "Communication" among tens of courses but different code "COM10", "COM11", "COM12".

I want to paginate the complete list by 10 and these 3 courses are between the 9th and 11th position. One is missing and one is in double between the two pages.

I remember I had the same issue with testing data I had to avoid same creation date to keep my unit tests clean. We updated mongoose from 7.5.0 to 8.0.0 in last november.

Steps to Reproduce

const courses = await CourseModel.find().sort('title').limit(10).skip(0);
console.log(courses.map(({ title, code }) => `${title} ${code}`));

/* [
  ... 
  ... 
  ... 
  ... 
  ... 
  ... 
  ... 
  ... 
  ... 
  'Communication COM10'
] */

const courses = await CourseModel.find().sort('title').limit(10).skip(10);
console.log(courses.map(({ title, code }) => `${title} ${code}`));

/*
[
  'Communication COM10',
  'Communication COM12',
  'Communication 2 COM20',
  ....
]
*/

Expected Behavior

const courses = await CourseModel.find().sort('title').limit(10).skip(0);
console.log(courses.map(({ title, code }) => `${title} ${code}`));

/* [
  ... 
  ... 
  ... 
  ... 
  ... 
  ... 
  ... 
  ... 
  ... 
  'Communication COM10'
] */

const courses = await CourseModel.find().sort('title').limit(10).skip(10);
console.log(courses.map(({ title, code }) => `${title} ${code}`));

/*
[
  'Communication COM11',
  'Communication COM12',
  'Communication 2 COM20',
  'XXXX',
  'XXXY',
   ....
]
*/

Metadata

Metadata

Assignees

Labels

helpThis issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions