Overview
このページでは、 MongoDBからデータを読み取るのに使用できる一般的なScalaドライバー メソッドを示すコピー可能なコード例を紹介します。
Tip
このページに記載されているメソッドの詳細については、各セクションに提供されているリンクを参照してください。
このページの例を使用するには、コード サンプルをサンプル アプリケーションまたは独自のアプリケーションにコピーします。 <connection string URI>などのコード例にあるすべてのプレースホルダーを、MongoDB 配置に関連する値に置き換えてください。
サンプル アプリケーション
サンプル アプリケーション
次のサンプルアプリケーションを使用して、このページのコード例をテストできます。 サンプル アプリケーションを使用するには、次の手順を実行します。
Maven または s以降のプロジェクトにScalaドライバーがインストールされていることを確認してください。
次のコードをコピーし、新しい
.scalaファイルに貼り付けます。このページからコード例をコピーし、 ファイル内の指定された行に貼り付けます。
import org.mongodb.scala._import org.mongodb.scala.bson.Documentimport org.mongodb.scala.model.Filters._import org.mongodb.scala.model.changestream._
object SampleReadApp {
def main(args: Array[String]): Unit = { val mongoClient = MongoClient("<connection string URI>") val database: MongoDatabase = mongoClient.getDatabase("<database name>") val collection: MongoCollection[Document] = database.getCollection("<collection name>")
// Start example code here
// End example code here
// Wait for the operations to complete before closing client // Note: This example uses Thread.sleep() for brevity and does not guarantee all // operations will be completed in time Thread.sleep(1000) mongoClient.close() }}1件を特定
次の例では、指定されたフィルターで指定された条件に一致するドキュメントを検索します。
val filter = equal("<field>", "<value>")
collection.find(filter).first().subscribe((doc: Document) => println(doc.toJson()), (e: Throwable) => println(s"There was an error: $e"))first()メソッドの詳細については、「データの取得」ガイドを参照してください。
複数検索
次の例では、指定されたフィルターで指定された条件に一致するすべてのドキュメントを検索します。
val filter = equal("<field>", "<value>")
collection.find(filter).subscribe((doc: Document) => println(doc.toJson()), (e: Throwable) => println(s"There was an error: $e"))find()メソッドの詳細については、「データの取得」ガイドを参照してください。
コレクション内のドキュメントをカウント
次の例では、指定された コレクション内のドキュメントの数を返します。
collection.countDocuments() .subscribe((count: Long) => println(s"Number of documents: $count"), (e: Throwable) => println(s"There was an error: $e"))countDocuments()メソッドについて詳しくは、「ドキュメントのカウントガイド」を参照してください。
クエリから返されたドキュメントをカウントする
次の例では、指定されたコレクション内のクエリ条件に一致するドキュメントの数を返します。
collection.countDocuments(equal("<field>", "<value>")) .subscribe((count: Long) => println(s"Number of documents: $count"), (e: Throwable) => println(s"There was an error: $e"))countDocuments()メソッドについて詳しくは、「ドキュメントのカウントガイド」を参照してください。
推定ドキュメント数
次の例では、コレクションのメタデータに基づいて、指定されたコレクション内のドキュメントのおおよその数を返します。
collection.estimatedDocumentCount() .subscribe((count: Long) => println(s"Estimated number of documents: $count"), (e: Throwable) => println(s"There was an error: $e"))estimatedDocumentCount()メソッドについて詳しくは、「ドキュメントのカウントガイド」を参照してください。
Retrieve Distinct Values
次の例では、指定されたコレクション内の指定されたフィールド名のすべての個別の値を返します。
collection.distinct("<field>") .subscribe((value: String) => println(value), (e: Throwable) => println(s"There was an error: $e"))distinct()メソッドの詳細については、「個別のフィールド値の取得 」ガイドを参照してください。
データの変更を監視
次の例では、特定のコレクションの変更ストリームを作成し、そのコレクション内の後続の変更イベントを出力します。
val changeStreamObservable = collection.watch()
changeStreamObservable.subscribe( (changeEvent: ChangeStreamDocument[Document]) => { println(s"Received a change to the collection: ${changeEvent}") }, (e: Throwable) => { println(s"Encountered an error: ${e.getMessage}") }, () => println("Completed"))watch()メソッドの詳細については、「データの変更を監視 」のガイドを参照してください。