I am paginating through a very large collection of documents. I was wondering if this an efficient means of pagination in mongoDB using _id's. My concern is that every time I make the query the entire collection of id's would need to get sorted before a result could be returned. I saw this document on optimizing queries with indexes, but would this apply to me since Im querying on _id which I would've thought is already indexed?
Pagination function:
function paginateDocs(currId, docsPerPage) {
const query = currId ? { _id: { $gt: currId } } : {};
const queryOptions = { limit: usersPerPage, sort: '_id' };
return mongo.find(query, queryOptions).toArray();
}
You can use cursor.hint(index) where you can define index as what all indexes on which you want to sort on. Refer to this link-> https://docs.mongodb.com/manual/reference/method/cursor.hint/
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.