Framework/Springboot

[SpringBoot] kotlin + SpringBoot + mongodb MongoRepository์—์„œ limit ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•

yuri lee 2023. 2. 28. 18:31
๋ฐ˜์‘ํ˜•

Intro

์•ˆ๋…•ํ•˜์„ธ์š”. ์ด๋ฒˆ์‹œ๊ฐ„์—๋Š” ์Šคํ”„๋ง๋ถ€ํŠธ + mongodb MongoRepository์—์„œ limit ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 
 

How to solve the problem

์•„๋ž˜ ์ฝ”๋“œ์™€ ๊ฐ™์ด limit() ๋ฉ”์„œ๋“œ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์›ํ•˜๋Š” count ๋งŒํผ ์กฐํšŒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 

suspend fun getRecentData(createAt: Instant): List<SomeDTO>? {
    val query = Query()
    query.with(
        Sort.by(Sort.Direction.DESC, "createAt")
    )
    query.limit(3)
    return metaHotMongoTemplate.find(query, SomeDTO::class.java, collectionName).collectList()
        .awaitSingle()
}
๋ฐ˜์‘ํ˜•