问题
Summary: I want to use the 'skip' function used in MySql and MongoDb. However, 'startAfter (number)' does not skip.
I am making a bulletin board now. Then I got a question.
This is a matter of page skip.
I use the basic method of moving pages directly to users.
However, I could not find any skip functions that Mysql or MongoDB used in the past.
This is part of my code.
return db.collection('boards').doc(title).collection('posts')
.orderBy('date').startAfter(2).limit(10).get()
So I want to skip two data from the data sorted by date.
However, any number in 'startAfter' will be taken from the first data.
Of course, I checked the following code in the official document.
return first.get().then(function (documentSnapshots) {
// Get the last visible document
var lastVisible = documentSnapshots.docs[documentSnapshots.docs.length-1];
console.log("last", lastVisible);
// Construct a new query starting at this document,
// get the next 25 cities.
var next = db.collection("cities")
.orderBy("population")
.startAfter(lastVisible)
.limit(25);
});
However, the above method is very troublesome because it is possible to have existing data unconditionally.
Is not there a good way? I am waiting for a reply.
回答1:
Client SDKs
The documentation for this states:
fieldValuesOrDocumentSnapshot
(repeatable any type or DocumentSnapshot)
The snapshot of the document the query results should start after or the field values to start this query after, in order of the query's order by.
Therefore, you are unable to simply specify the number of documents to skip. You will either need to know a value from that second document (ID, field value, etc.) or get the first 2 documents, then use the snapshot of the second document, to generate your query.
Admin SDK
With the Admin SDK, there is the offset method, which should allow you to achieve what you are looking for.
来源:https://stackoverflow.com/questions/50330808/does-firestore-support-skip-functionality