Overview
このガイドでは、 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 操作
find()メソッドは、コレクションからドキュメントを取得します。 このメソッドはクエリフィルターを受け取り、一致するすべてのドキュメントを返します。 クエリフィルターは、ドライバーがコレクションのドキュメントを照合するために使用する基準を指定するドキュメントです。
クエリフィルターの詳細については、「 クエリの指定」ガイドを参照してください。
1 ドキュメントの特定の例
次の例では、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() メソッドの動作を変更できます。次の表では、クエリを変更するために一般的に使用される方法について説明します。
方式 | 説明 |
|---|---|
| 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. |
| Processes each element in the flow and applies the given lambda. |
| 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. |
| Returns the first document that matches the query or throws a MongoClientException
if no matching documents exist. |
| Returns the first document that matches the query or null if no matching documents exist. |
| Specifies the index to use for the query. |
| Limits the number of documents to be returned from the query. |
| Sets the number of documents to skip. |
| 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 ドキュメント
このガイドで説明したメソッドや型の詳細については、次の API ドキュメントを参照してください。