Docs Menu
Docs Home
/ /

문서 찾기

이 가이드 에서는 코틀린 (Kotlin) 운전자 사용하여 읽기 작업을 통해 MongoDB 컬렉션 에서 데이터를 조회 방법을 학습 수 있습니다. find() 메서드를 호출하여 쿼리 필터에 지정된 기준 설정하다 와 일치하는 문서를 조회 할 수 있습니다.

이 가이드 의 예제에서는 Atlas 샘플 데이터 세트sample_mflix 데이터베이스 에 있는 movies 컬렉션 사용합니다. 무료 MongoDB Atlas 클러스터 생성하고 샘플 데이터 세트를 로드하는 방법을 학습하려면 시작하기 가이드 참조하세요.

다음 코틀린 (Kotlin) 데이터 클래스는 이 컬렉션 의 문서를 모델링합니다.

data class Movie(val title: String, val runtime: Int, val imdb: IMDB){
data class IMDB(val rating: Double)
}

다음 코틀린 (Kotlin) 데이터 클래스는 이 가이드 의 예시에서 반환된 결과를 모델링합니다.

data class Results(val title: String)

find() 메서드는 컬렉션 에서 문서를 검색합니다. 이 메서드는 쿼리 필터하다 를 사용하여 일치하는 모든 문서를 반환합니다. 쿼리 필터하다 는 운전자 가 컬렉션 의 문서를 일치시키는 데 사용하는 기준을 지정하는 문서 입니다.

쿼리 필터에 학습 보려면 쿼리 지정 가이드 를 참조하세요.

다음 예시 에서는 firstOrNull() 메서드를 find() 메서드 호출에 연결하여 name 필드 의 값이 "The Room"인 첫 번째 문서 찾습니다.

val projectionFields= Projections.fields(
Projections.include(Movie::title.name, Movie::imdb.name),
Projections.excludeId()
)
val resultsFlow = collection.withDocumentClass<Results>()
.find(eq(Movie::title.name, "The Room"))
.projection(projectionFields)
.firstOrNull()
if (resultsFlow == null) {
println("No results found.");
} else {
println(resultsFlow)
}
Movie(title=The Room, runtime=99, imdb=IMDB(rating=3.7))

이 예시 find() 메서드를 사용하여 runtime 필드 의 값이 15보다 작은 모든 문서를 찾습니다. find() 작업은 FindFlow 객체 반환하며, 다음 예시 와 같이 collect() 메서드를 사용하여 반복할 수 있습니다.

val projectionFields= Projections.fields(
Projections.include(Movie::title.name, Movie::imdb.name),
Projections.excludeId()
)
val resultsFlow = collection.withDocumentClass<Results>()
.find(lt(Movie::runtime.name, 15))
.projection(projectionFields)
resultsFlow.collect { println(it) }
Movie(title=Meshes of the Afternoon, runtime=14, imdb=IMDB(rating=8.0))
Movie(title=The Band Concert, runtime=9, imdb=IMDB(rating=7.9))
Movie(title=What's Opera, Doc?, runtime=7, imdb=IMDB(rating=8.3))
...

참고

모든 문서 찾기

컬렉션의 모든 문서를 찾으려면 find() 메서드에 빈 필터를 전달합니다.

val resultsFlow = collection.withDocumentClass<Results>()
.find()

메서드를 FindFlow 인스턴스 에 연결하여 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.

collect()

Processes each element in the flow and applies the given lambda.

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.

first()

Returns the first document that matches the query or throws a MongoClientException if no matching documents exist.

firstOrNull()

Returns the first document that matches the query or null if no matching documents exist.

hint()

Specifies the index to use for the query.

limit()

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

skip()

Sets the number of documents to skip.

sort()

Defines the sort criteria to apply to the query.

다음 예시 에서는 limit() 메서드를 연결하여 쿼리 에서 반환되는 문서 수를 10(으)로 제한합니다.

val projectionFields= Projections.fields(
Projections.include(Movie::title.name, Movie::imdb.name),
Projections.excludeId()
)
val resultsFlow = collection.withDocumentClass<Results>()
.find()
.projection(projectionFields)
.limit(10)

find()의 동작을 수정하는 전체 메서드 목록은 Kotlin 드라이버 API 문서의 FindFlow 클래스를 참조하세요.

쿼리 필터에 대해 자세히 알아보려면 쿼리 지정을 참조하세요.

이 가이드에서 사용되는 메서드 또는 유형에 대해 자세히 알아보려면 다음 API 설명서를 참조하세요.

돌아가기

문서 삽입

이 페이지의 내용