How to implement Pagination with Ktor and Kmongo

1.4k Views Asked by At

Problem: To Implement Pagination on the server side using Ktor and Kmongo.

Tech Stacks:

Kotlin as Programming Language.

Ktor as Web Framework.

MongoDb as database.

Kmongo as Sql framework.

I cannot find any tutorial or posts that describes to implement pagination with Database in Ktor.

1

There are 1 best solutions below

0
On BEST ANSWER

This is how I implemented the same.

override suspend fun getAllUsers(page: Int, limit: Int): List<User> {
    return database.getCollection<User>().find().skip(skip = (page - 1) * limit).limit(limit = limit)
        .partial(true).descendingSort(User::lastLoginTime).toList()
}

Thanks to @Aleksei Tirman.

For More see - https://stackoverflow.com/a/15387497/13963150