개요
이 가이드에서는 MongoDB 코틀린(Kotlin) 드라이버에서 쿼리를 지정하는 방법을 배울 수 있습니다.
대부분의 CRUD 작업은 쿼리 필터 에 일치 조건을 지정하여 일치하는 문서 세트의 범위를 좁힐 수 있습니다. 쿼리 필터에는 결과 세트에 포함할 문서를 결정하는 특정 필드에 적용되는 쿼리 연산자가 하나 이상 포함되어 있습니다.
이 페이지에서는 다음 쿼리 연산자를 사용 방법에 대한 예제를 통해 다룹니다.
이 가이드의 예에서는 paint_purchases
컬렉션의 다음 문서를 사용합니다.
{ "_id": 1, "color": "red", "qty": 9, "vendor": ["A", "E"] } { "_id": 2, "color": "purple", "qty": 8, "vendor": ["B", "D", "F"], "rating": 5 } { "_id": 3, "color": "blue", "qty": 5, "vendor": ["A", "E"] } { "_id": 4, "color": "white", "qty": 6, "vendor": ["D"], "rating": 9 } { "_id": 5, "color": "yellow", "qty": 4, "vendor": ["A", "B"] } { "_id": 6, "color": "pink", "qty": 3, "vendor": ["C"] } { "_id": 7, "color": "green", "qty": 8, "vendor": ["C", "E"], "rating": 7 } { "_id": 8, "color": "black", "qty": 7, "vendor": ["A", "C", "D"] }
이 데이터는 다음 Kotlin 데이터 클래스로 모델링됩니다.
data class PaintOrder( val id: Int, val qty: Int, val color: String, val vendor: List<String>, val rating: Int? = null )
참고
쿼리 필터 클래스
The examples in this guide use the Filters class from the com.mongodb.client.model
package to create query filters. Alternatively, you can use the Filters class from the Kotlin driver's mongodb-driver-kotlin-extensions
extension library, which allows you to create query filters that recognize serialization annotations.
To view an example that uses the extension library's Filters
class, see Query Annotated Classes in the Kotlin Serialization guide.
비교 연산자
비교 연산자는 collection의 값과의 비교를 기반으로 데이터를 쿼리합니다. 일반적인 비교 연산자로는 '보다 큰(greater than)' 비교를 위한 gt()
, '보다 작은(less than)' 비교를 위한 lte()
, '같지 않은(not equal to)' 비교를 위한 ne()
이(가) 있습니다.
다음 예제에서는 메서드를 사용하여 Filters.gt()
qty
7
paint_purchases
collection에서 값이 보다 큰 모든 문서를 일치시킵니다.
val filter = Filters.gt("qty", 7) val findFlow = collection.find(filter) findFlow.collect { println(it) }
PaintOrder(id=1, qty=9, color=red, vendor=[A, E], rating=null) PaintOrder(id=2, qty=8, color=purple, vendor=[B, D, F], rating=5) PaintOrder(id=7, qty=8, color=green, vendor=[C, E], rating=7)
로직 연산
논리 연산자는 필드 수준 연산자의 결과에 적용된 논리를 사용하여 데이터를 쿼리합니다. 일반적인 논리 연산자로는 모든 연산자가 참이어야 하는 and()
, 연산자 중 하나 이상이 참이어야 하는 or()
등이 있습니다.
다음 예제에서는 Filters.and()
메서드를 사용하여 qty
5
color
"pink"
paint_purchases
collection에서 값이 보다 작거나 같고 이 아닌 문서를 일치시킵니다.
val filter = Filters.and(Filters.lte("qty", 5), Filters.ne("color", "pink")) val findFlow = collection.find(filter) findFlow.collect { println(it) }
PaintOrder(id=3, qty=5, color=blue, vendor=[A, E], rating=null) PaintOrder(id=5, qty=4, color=yellow, vendor=[A, B], rating=null)
배열 연산자
배열 연산자는 배열 필드에 있는 요소의 값 또는 수량을 기반으로 데이터를 쿼리합니다.
다음 예제에서는 Filters.size()
메서드를 사용하여 paint_purchases
컬렉션에서 vendor
목록의 크기가 3
인 문서를 일치시킵니다.
val filter = Filters.size("vendor", 3) val findFlow = collection.find(filter) findFlow.collect { println(it) }
PaintOrder(id=2, qty=8, color=purple, vendor=[B, D, F], rating=5) PaintOrder(id=8, qty=7, color=black, vendor=[A, C, D], rating=null)
요소 연산자
요소 연산자는 필드의 존재 여부 또는 유형에 따라 데이터를 쿼리합니다.
다음 예제에서는 Filters.exists()
메서드를 사용하여 rating
paint_purchases
collection에 가 있는 문서를 일치시킵니다.
val filter = Filters.exists("rating") val findFlow = collection.find(filter) findFlow.collect { println(it) }
PaintOrder(id=2, qty=8, color=purple, vendor=[B, D, F], rating=5) PaintOrder(id=4, qty=6, color=white, vendor=[D], rating=9) PaintOrder(id=7, qty=8, color=green, vendor=[C, E], rating=7)
평가 연산자
평가 연산자는 정규식 및 텍스트 검색과 같은 상위 수준의 로직에서 데이터를 쿼리합니다.
다음 예제에서는 Filters.regex()
메서드를 사용하여 color
"k"
paint_purchases
collection에서 문자 로 끝나는 문서 가 있는 문서를 일치시킵니다.
val filter = Filters.regex("color", "k$") val findFlow = collection.find(filter) findFlow.collect { println(it) }
PaintOrder(id=6, qty=3, color=pink, vendor=[C], rating=null) PaintOrder(id=8, qty=7, color=black, vendor=[A, C, D], rating=null)
이 가이드에 언급된 연산자에 대한 자세한 내용은 다음 서버 매뉴얼 항목을 참조하세요.