Docs Menu
Docs Home
/ / /
Scala
/

Retrieve Data

項目一覧

  • Overview
  • サンプル データ
  • ドキュメントの検索
  • 複数ドキュメントの検索
  • 1 つのドキュメントの検索
  • 検索動作の変更
  • 詳細情報
  • API ドキュメント

このガイドでは、 Scalaドライバーを使用して、読み取り操作 を使用してMongoDBコレクションからデータを検索する方法を学習できます。コレクションで find()メソッドを呼び出して、基準のセットに一致するドキュメントを取得できます。

このガイドの例では、companies sample_trainingAtlasサンプルデータセット の データベースの コレクションを使用します。 Scalaアプリケーションからこのコレクションにアクセスするには、AtlasMongoClient クラスターに接続する を作成し、 変数と 変数に次の値を割り当てます。databasecollection

val database: MongoDatabase = mongoClient.getDatabase("sample_training")
val collection: MongoCollection[Document] = database.getCollection("companies")

MongoDB Atlasクラスターを無料で作成して、サンプルデータセットをロードする方法については、 「Atlas を使い始める」ガイドを参照してください。

コレクションからドキュメントを検索するには、find() メソッドを使用します。このメソッドはクエリフィルターパラメーターを受け取り、クエリ結果にアクセスできる FindObservableクラスのインスタンスを返します。FindObservable クラスには、FindObservable インスタンスに連結してその動作を変更できる追加のメソッドも用意されています( など)。first()

Tip

クエリフィルターの詳細については、「 クエリの指定」ガイドを参照してください。

コレクション内の複数のドキュメントを検索するには、検索するドキュメントの基準を指定するクエリフィルターを find() メソッドに渡します。

find() メソッドは FindObservable のインスタンスを返します。これを反復処理して一致するドキュメントを確認できます。 subscribe() メソッドを使用して FindObservable を反復処理します。

次の例では、 find() メソッドを使用して、founded_yearフィールドの値が 1970 であるすべてのドキュメントを検索し、その結果を出力します。

val filter = equal("founded_year", 1970)
collection.find(filter).subscribe((doc: Document) => println(doc.toJson()),
(e: Throwable) => println(s"There was an error: $e"))
{"_id":{"$oid":"..."},"name":"Mitsubishi Motors","permalink":"mitsubishi-motors",
"crunchbase_url":"http://www.crunchbase.com/company/mitsubishi-motors",
... }
{"_id":{"$oid":"..."},"name":"Western Digital","permalink":"western-digital",
"crunchbase_url":"http://www.crunchbase.com/company/western-digital",
... }
{"_id":{"$oid":"..."},"name":"Celarayn","permalink":"celarayn","crunchbase_url":
"http://www.crunchbase.com/company/celarayn",
... }

注意

すべてのドキュメントの検索

コレクション内のすべてのドキュメントを検索するには、パラメータを渡しずに find() メソッドを呼び出します。

collection.find()

コレクション内の 1 つのドキュメントを検索するには、find() メソッドを呼び出し、検索するドキュメントの基準を指定するクエリフィルターを渡します。次に、first() メソッドを find() にチェーンします。

find() メソッドは FindObservableインスタンスを返し、first() メソッドは FindObservable によって保存された最初のクエリ結果を含む SingleObserverインスタンスを返します。 subscribe() メソッドを呼び出すと、SingleObserver の結果にアクセスできます。

次の例では、 find() メソッドと first() メソッドを使用して、nameフィールドの値が "LinkedIn" である最初のドキュメントを検索します。

val filter = equal("name", "LinkedIn")
collection.find(filter).first().subscribe((doc: Document) => println(doc.toJson()),
(e: Throwable) => println(s"There was an error: $e"))
{"_id": {"$oid": "..."}, "name": "LinkedIn", "permalink": "linkedin", "crunchbase_url":
"http://www.crunchbase.com/company/linkedin", "homepage_url": "http://linkedin.com",
...}

Tip

並び替え順

ソート条件が指定されていない場合、 first()メソッドはディスク上の自然な順序で最初のドキュメントを返します。

FindObservableクラスによって提供されるメソッドを連鎖させることで、find() メソッドの動作を変更できます。次の表では、これらの方法の一部について説明しています。

方式
説明

explain()

Explains the execution plan for this operation with the specified verbosity level.
Parameter Type: ExplainVerbosity

collation()

Sets the collation to use for the operation. The default value is the collation specified for the collection.
Parameter Type: Collation

comment()

Attaches a comment to the operation.
Parameter Type: String

first()

Returns an Observable that stores only the first query result. To view an example that uses this method, see Find One Document on this page.

limit()

Sets the maximum number of documents the operation can return.
Parameter Type: Int

skip()

Sets the number of documents to skip before returning results.
Parameter Type: Int

sort()

Sets the order in which the operation returns matching documents.
Parameter Type: Bson

次の例では、 find() メソッドを使用して、number_of_employeesフィールドの値が 1000 であるすべてのドキュメントを検索します。この例ではlimit() メソッドを使用して最大 5 の結果を返します。

val filter = equal("number_of_employees", 1000)
collection.find(filter).limit(5).subscribe((doc: Document) => println(doc.toJson()),
(e: Throwable) => println(s"There was an error: $e"))
{"_id": {"$oid": "..."}, "name": "Akamai Technologies", "permalink": "akamai-technologies",
"crunchbase_url": "http://www.crunchbase.com/company/akamai-technologies", "homepage_url":
"http://www.akamai.com", ... }
{"_id": {"$oid": "..."}, "name": "Yodle", "permalink": "yodle", "crunchbase_url":
"http://www.crunchbase.com/company/yodle", "homepage_url": "http://www.yodle.com", ... }
{"_id": {"$oid": "..."}, "name": "Antal International", "permalink": "antal-international",
"crunchbase_url": "http://www.crunchbase.com/company/antal-international", "homepage_url":
"http://antal.com", ... }
{"_id": {"$oid": "..."}, "name": "Yatra online", "permalink": "yatra-online", "crunchbase_url":
"http://www.crunchbase.com/company/yatra-online", "homepage_url": "http://www.Yatra.com", ... }
{"_id": {"$oid": "..."}, "name": "Gumtree", "permalink": "gumtree", "crunchbase_url":
"http://www.crunchbase.com/company/gumtree", "homepage_url": "http://www.gumtree.co.za", ... }

FindObservableメンバー メソッドの完全なリストについては、 FindObservable クラスのAPIドキュメントを参照してください。

クエリフィルターの詳細については、「クエリの指定」ガイドを参照してください。

Scalaドライバーを使用してドキュメントを取得するコード例については、 「 MongoDBからのデータの読み取り 」を参照してください。

このガイドで説明されているメソッドの詳細については、次の API ドキュメントを参照してください。

  • find()

  • first()

  • limit()

戻る

Read Data