Retrieve Data
Overview
このガイドでは、 Scalaドライバーを使用して、読み取り操作 を使用してMongoDBコレクションからデータを検索する方法を学習できます。コレクションで find()
メソッドを呼び出して、基準のセットに一致するドキュメントを取得できます。
サンプル データ
このガイドの例では、companies
sample_training
Atlasサンプルデータセット の データベースの コレクションを使用します。 Scalaアプリケーションからこのコレクションにアクセスするには、AtlasMongoClient
クラスターに接続する を作成し、 変数と 変数に次の値を割り当てます。database
collection
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 つのドキュメントの検索
コレクション内の 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", ...}
検索動作の変更
FindObservable
クラスによって提供されるメソッドを連鎖させることで、find()
メソッドの動作を変更できます。次の表では、これらの方法の一部について説明しています。
方式 | 説明 |
---|---|
| Explains the execution plan for this operation with the specified verbosity level. Parameter Type: ExplainVerbosity |
| Sets the collation to use for the operation. The default value is the collation
specified for the collection. Parameter Type: Collation |
| Attaches a comment to the operation. Parameter Type: String |
| 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. |
| Sets the maximum number of documents the operation can return. Parameter Type: Int |
| Sets the number of documents to skip before returning results. Parameter Type: Int |
| 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 ドキュメント
このガイドで説明されているメソッドの詳細については、次の API ドキュメントを参照してください。