'How to implement Pagination with Ktor and Kmongo

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.



Solution 1:[1]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Adi