Skip to content

Pagination #1

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

Open
Dark-Knight11 opened this issue Jun 4, 2023 · 0 comments
Open

Pagination #1

Dark-Knight11 opened this issue Jun 4, 2023 · 0 comments

Comments

@Dark-Knight11
Copy link

Hi, I was referring this project to implement pagination in my own project. I wanted to ask is there anyway to merge the count and data fetching queries.

override suspend fun tweets(
sentiment: SpecificSentiment,
page: Int,
pageSize: Int
): PagedData<List<ElaboratedTweet>> = coroutineScope {
val totalPages = async {
ceil(tweetsCollection.countDocuments("{sentiment: \"$sentiment\"}") / pageSize.toFloat())
.toInt()
}
val data = async {
tweetsCollection.find("{sentiment: \"$sentiment\"}")
.skip(page * pageSize)
.limit(pageSize)
.toList()
}
PagedData(data.await(), page, pageSize, totalPages.await())
}

Here you are traversing the tweetsCollection 2 times - once for the total pages count and another time for fetching data. Is there anyway to do it in single query.

I tried this but it isn't working

val result = transactions.aggregate<Test>(
    facet(
        Facet("count",
            count()
        ),
        Facet(
            "transactions", listOf(
                skip((pageNo - 1) * size),
                limit(size)
            )
        )
    )
).first()
println(result.transactions)
println(result.count)

data class Test(val count: Long, val transactions: List<Transaction>?)

I get this error

Cannot deserialize value of type `long` from Array value (token `JsonToken.START_ARRAY`)\n at [Source: de.undercouch.bson4jackson.io.LittleEndianInputStream@11c18e82; pos: 7] (through reference chain: TransactionRepository$Test[\"count\"])

you have any idea on it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant