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 ドキュメントを参照してください。

戻る

ドキュメントの挿入

項目一覧