Skip to content

Firestore limitToLast with endBefore query contains 1 less document #6134

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
xxdd13 opened this issue Apr 11, 2022 · 1 comment · Fixed by #6168
Closed

Firestore limitToLast with endBefore query contains 1 less document #6134

xxdd13 opened this issue Apr 11, 2022 · 1 comment · Fixed by #6168
Assignees

Comments

@xxdd13
Copy link

xxdd13 commented Apr 11, 2022

[REQUIRED] Describe your environment

  • Operating System version: macOS 12.3
  • Browser version: Version 100.0.4896.75 (Official Build) (arm64)
  • Firebase SDK version: 9.6.10
  • Firebase Product: firestore

[REQUIRED] Describe the problem

When using endBefore(cursor) and limitToLast(n), the snapshot contains n-1 documents

Steps to reproduce:

  1. Add some data to a collection with timestamp
  2. query, orderBy timestamp, limit 3
  3. load next page
  4. load prev page with endBefore and limitToLast
  5. snapshot contains 1 less doc

Relevant Code:

const app = initializeApp({
  //
});
const db = getFirestore(app);
const ref = collection(db, 'tests');

// add test data
const batch = writeBatch(db);
[
  '2022-04-01',
  '2022-04-02',
  '2022-04-03',
  '2022-04-04',
  '2022-04-05',
  '2022-04-06',
].forEach((date) => {
  batch.set(doc(ref, date), {
    timestamp: new Date(date),
    date,
  });
});
await batch.commit();

const page1 = await getDocs(query(ref, orderBy('timestamp'), limit(3))).then(
  (snap) => {
    console.log(
      'page 1',
      snap.docs.map((doc) => doc.data().date)
    );
    return snap.docs;
  }
);

// get next page
const page2 = await getDocs(
  query(
    ref,
    orderBy('timestamp'),
    limit(3),
    startAfter(page1[page1.length - 1])
  )
).then((snap) => {
  console.log(
    'page 2',
    snap.docs.map((doc) => doc.data().date)
  );
  return snap.docs;
});

// get prev page
const shouldBePage1 = await getDocs(
  query(ref, orderBy('timestamp'), limitToLast(3), endBefore(page2[0]))
).then((snap) => {
  console.log(
    'should be page 1',
    snap.docs.map((doc) => doc.data().date)
  );
  return snap.docs;
});
/*
output
page 1          ['2022-04-01', '2022-04-02', '2022-04-03']
page 2          ['2022-04-04', '2022-04-05', '2022-04-06']
should be page1 ['2022-04-02', '2022-04-03']

*/

if I use the regular limit, the size of snapshot is correct

query(ref, orderBy('timestamp'), limit(3), endBefore(page2[0]))
@cherylEnkidu
Copy link
Contributor

Hi xxdd13,
Thanks for your report! I will do some investigations and keep you updated.

@firebase firebase locked and limited conversation to collaborators May 21, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants