Docs Menu

Retrieve Data

์ด ๊ฐ€์ด๋“œ ์—์„œ๋Š” ์ฝ”ํ‹€๋ฆฐ ๋™๊ธฐ (Kotlin Sync) ์šด์ „์ž ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ฝ๊ธฐ ์ž‘์—…์„ ํ†ตํ•ด MongoDB ์ปฌ๋ ‰์…˜ ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒ ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ํ•™์Šต ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. find() ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์ฟผ๋ฆฌ ํ•„ํ„ฐํ•˜๋‹ค ์— ์ง€์ •๋œ ๊ธฐ์ค€ ์„ค์ •ํ•˜๋‹ค ์™€ ์ผ์น˜ํ•˜๋Š” ๋ฌธ์„œ๋ฅผ ์กฐํšŒ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด ๊ฐ€์ด๋“œ ์˜ ์˜ˆ์ œ์—์„œ๋Š” Atlas ์ƒ˜ํ”Œ ๋ฐ์ดํ„ฐ ์„ธํŠธ ์˜ sample_restaurants ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์— ์žˆ๋Š” restaurants ์ปฌ๋ ‰์…˜ ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋ฌด๋ฃŒ MongoDB Atlas cluster ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์ƒ˜ํ”Œ ๋ฐ์ดํ„ฐ ์„ธํŠธ๋ฅผ ๋กœ๋“œํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ํ•™์Šต ๋ณด๋ ค๋ฉด Atlas ์‹œ์ž‘ํ•˜๊ธฐ ๊ฐ€์ด๋“œ ๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”.

์ด ์ปฌ๋ ‰์…˜ ์˜ ๋ฌธ์„œ๋Š” ๋‹ค์Œ ์ฝ”ํ‹€๋ฆฐ (Kotlin) ๋ฐ์ดํ„ฐ ํด๋ž˜์Šค์— ๋”ฐ๋ผ ๋ชจ๋ธ๋ง๋ฉ๋‹ˆ๋‹ค.

data class Restaurant(
val name: String,
val cuisine: String
)

find() ๋ฉ”์„œ๋“œ๋Š” ์ปฌ๋ ‰์…˜ ์—์„œ ๋ฌธ์„œ๋ฅผ ๊ฒ€์ƒ‰ํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ฉ”์„œ๋“œ๋Š” ์ฟผ๋ฆฌ ํ•„ํ„ฐํ•˜๋‹ค ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ผ์น˜ํ•˜๋Š” ๋ชจ๋“  ๋ฌธ์„œ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ฟผ๋ฆฌ ํ•„ํ„ฐํ•˜๋‹ค ๋Š” ์šด์ „์ž ๊ฐ€ ์ปฌ๋ ‰์…˜ ์˜ ๋ฌธ์„œ๋ฅผ ์ผ์น˜์‹œํ‚ค๋Š” ๋ฐ ์‚ฌ์šฉํ•˜๋Š” ๊ธฐ์ค€์„ ์ง€์ •ํ•˜๋Š” ๋ฌธ์„œ ์ž…๋‹ˆ๋‹ค.

์ฟผ๋ฆฌ ํ•„ํ„ฐ์— ํ•™์Šต ๋ณด๋ ค๋ฉด ์ฟผ๋ฆฌ ์ง€์ • ๊ฐ€์ด๋“œ ๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”.

๋‹ค์Œ ์˜ˆ์‹œ ์—์„œ๋Š” find() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ cuisine ํ•„๋“œ ์˜ ๊ฐ’์ด "Spanish" ์ธ ๋ชจ๋“  ๋ฌธ์„œ๋ฅผ ์ฐพ์Šต๋‹ˆ๋‹ค.

val results = collection.find(eq(Restaurant::cuisine.name, "Spanish"))

์•ž์˜ ์˜ˆ์‹œ ์—์„œ find() ์ž‘์—…์€ FindIterable ๊ฐ์ฒด ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฉฐ, ๋‹ค์Œ ์˜ˆ์‹œ ์™€ ๊ฐ™์ด forEach() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ˜๋ณตํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

val results = collection.find(eq(Restaurant::cuisine.name, "Spanish"))
results.forEach { result ->
println(result)
}
Restaurant(name=Tropicoso Club, cuisine=Spanish)
Restaurant(name=Beso, cuisine=Spanish)
Restaurant(name=Sabor Latino Restaurant, cuisine=Spanish)
...

์ฐธ๊ณ 

๋ชจ๋“  ๋ฌธ์„œ ์ฐพ๊ธฐ

์ปฌ๋ ‰์…˜์˜ ๋ชจ๋“  ๋ฌธ์„œ๋ฅผ ์ฐพ์œผ๋ ค๋ฉด find() ๋ฉ”์„œ๋“œ์— ๋นˆ ํ•„ํ„ฐ๋ฅผ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค.

val results = collection.find()

๋ฉ”์„œ๋“œ๋ฅผ find() ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ์— ์—ฐ๊ฒฐํ•˜์—ฌ find() ๋ฉ”์„œ๋“œ์˜ ๋™์ž‘์„ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ ํ‘œ์—์„œ๋Š” ์ฟผ๋ฆฌ ์ˆ˜์ •์— ์ผ๋ฐ˜์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ๋ฉ”์„œ๋“œ์— ๋Œ€ํ•ด ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค.

๋ฉ”์„œ๋“œ
์„ค๋ช…

batchSize()

Limits the number of documents to return per batch. To learn more about batch size, see cursor.batchSize() in the MongoDB Server manual.

collation()

Sets the collation options for the query.

comment()

Specifies a string to attach to the query. This can help you trace and interpret the operation in the server logs and in profile data. To learn more about query comments, see $comment in the MongoDB Server manual.

hint()

Specifies the index to use for the query.

limit()

Limits the number of documents to be returned from the query.

maxTime()

Sets the maximum execution time on the server for this operation.

skip()

Sets the number of documents to skip.

sort()

Defines the sort criteria to apply to the query.

๋‹ค์Œ ์˜ˆ์‹œ ์—์„œ๋Š” limit() ๋ฐ maxTime() ๋ฉ”์„œ๋“œ๋ฅผ ์—ฐ๊ฒฐํ•˜์—ฌ ์ฟผ๋ฆฌ ์—์„œ ๋ฐ˜ํ™˜๋˜๋Š” ๋ฌธ์„œ ์ˆ˜๋ฅผ 10 ๋กœ ์ œํ•œํ•˜๊ณ  ์ž‘์—…์˜ ์ตœ๋Œ€ ์‹คํ–‰ ์‹œ๊ฐ„์„ 10000 ๋ฐ€๋ฆฌ์ดˆ๋กœ ์„ค์ •ํ•˜๋‹ค ํ•ฉ๋‹ˆ๋‹ค.

val results = collection
.find(eq(Restaurant::cuisine.name, "Spanish"))
.limit(10)
.maxTime(10000)

์˜ ๋™์ž‘์„ ์ˆ˜์ •ํ•˜๋Š” find() ๋ฉ”์„œ๋“œ์˜ ์ „์ฒด ๋ชฉ๋ก์€ ํด๋ž˜์Šค์— ๋Œ€ํ•œ API ๋ฌธ์„œ ๋ฅผ FindIterable ์ฐธ์กฐํ•˜์„ธ์š”.

์ฟผ๋ฆฌ ํ•„ํ„ฐ์— ๋Œ€ํ•ด ์ž์„ธํžˆ ์•Œ์•„๋ณด๋ ค๋ฉด ์ฟผ๋ฆฌ ์ง€์ •์„ ์ฐธ์กฐํ•˜์„ธ์š”.

์ฝ”ํ‹€๋ฆฐ ๋™๊ธฐ (Kotlin Sync) ์šด์ „์ž ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์„œ๋ฅผ ์กฐํšŒ ํ•˜๋Š” ์‹คํ–‰ ๊ฐ€๋Šฅํ•œ ์ฝ”๋“œ ์˜ˆ์ œ๋ฅผ ๋ณด๋ ค๋ฉด MongoDB ์—์„œ ๋ฐ์ดํ„ฐ ์ฝ๊ธฐ๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”.

์ด ๊ฐ€์ด๋“œ์—์„œ ์‚ฌ์šฉ๋˜๋Š” ๋ฉ”์„œ๋“œ ๋˜๋Š” ์œ ํ˜•์— ๋Œ€ํ•ด ์ž์„ธํžˆ ์•Œ์•„๋ณด๋ ค๋ฉด ๋‹ค์Œ API ์„ค๋ช…์„œ๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”.