Docs Menu

Read Data

このページでは、 MongoDBからデータを読み取るのに使用できる一般的なScalaドライバー メソッドを示すコピー可能なコード例を紹介します。

Tip

このページに記載されているメソッドの詳細については、各セクションに提供されているリンクを参照してください。

このページの例を使用するには、コード サンプルをサンプル アプリケーションまたは独自のアプリケーションにコピーします。 <connection string URI>などのコード例にあるすべてのプレースホルダーを、MongoDB 配置に関連する値に置き換えてください。

次のサンプルアプリケーションを使用して、このページのコード例をテストできます。 サンプル アプリケーションを使用するには、次の手順を実行します。

  1. Maven または s以降のプロジェクトにScalaドライバーがインストールされていることを確認してください。

  2. 次のコードをコピーし、新しい.scalaファイルに貼り付けます。

  3. このページからコード例をコピーし、 ファイル内の指定された行に貼り付けます。

1import org.mongodb.scala._
2import org.mongodb.scala.bson.Document
3import org.mongodb.scala.model.Filters._
4import org.mongodb.scala.model.changestream._
5
6object SampleReadApp {
7
8 def main(args: Array[String]): Unit = {
9 val mongoClient = MongoClient("<connection string URI>")
10 val database: MongoDatabase = mongoClient.getDatabase("<database name>")
11 val collection: MongoCollection[Document] = database.getCollection("<collection name>")
12
13
14 // Start example code here
15
16 // End example code here
17
18 // Wait for the operations to complete before closing client
19 // Note: This example uses Thread.sleep() for brevity and does not guarantee all
20 // operations will be completed in time
21 Thread.sleep(1000)
22 mongoClient.close()
23 }
24}

次の例では、指定されたフィルターで指定された条件に一致するドキュメントを検索します。

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()メソッドについて詳しくは、「ドキュメントのカウントガイド」を参照してください。

次の例では、指定されたコレクション内の指定されたフィールド名のすべての個別の値を返します。

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()メソッドの詳細については、「データの変更を監視 」のガイドを参照してください。