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()
๋ฉ์๋์ ๋์์ ์์ ํ ์ ์์ต๋๋ค. ๋ค์ ํ์์๋ ์ฟผ๋ฆฌ ์์ ์ ์ผ๋ฐ์ ์ผ๋ก ์ฌ์ฉ๋๋ ๋ฉ์๋์ ๋ํด ์ค๋ช
ํฉ๋๋ค.
๋ฉ์๋ | ์ค๋ช
|
---|---|
| Limits the number of documents to return per batch. To learn more about
batch size, see cursor.batchSize()
in the MongoDB Server manual. |
| Sets the collation options for the query. |
| 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. |
| Specifies the index to use for the query. |
| Limits the number of documents to be returned from the query. |
| Sets the maximum execution time on the server for this operation. |
| Sets the number of documents to skip. |
| 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 ๋ฌธ์
์ด ๊ฐ์ด๋์์ ์ฌ์ฉ๋๋ ๋ฉ์๋ ๋๋ ์ ํ์ ๋ํด ์์ธํ ์์๋ณด๋ ค๋ฉด ๋ค์ API ์ค๋ช ์๋ฅผ ์ฐธ์กฐํ์ธ์.