Retrieve Data
On this page
Overview
In this guide, you can learn how to use the Scala driver to retrieve
data from a MongoDB collection by using read operations. You can call the
find()
method on a collection to retrieve documents that match a set of
criteria.
Sample Data
The examples in this guide use the companies
collection in the sample_training
database from the Atlas sample datasets. To access this collection
from your Scala application, create a MongoClient
that connects to an Atlas cluster
and assign the following value to your database
and collection
variables:
val database: MongoDatabase = mongoClient.getDatabase("sample_training") val collection: MongoCollection[Document] = database.getCollection("companies")
To learn how to create a free MongoDB Atlas cluster and load the sample datasets, see the Get Started with Atlas guide.
Find Documents
To retrieve documents from a collection, use the find()
method. This method
takes a query filter parameter and returns an instance of the FindObservable
class
from which you can access the query results. The FindObservable
class also
provides additional methods that you can chain to a FindObservable
instance to modify
its behavior, such as first()
.
Tip
To learn more about query filters, see the Specify a Query guide.
Find Multiple Documents
To find multiple documents in a collection, pass a query filter to the
find()
method that specifies the criteria of the documents you want to
retrieve.
The find()
method returns an instance of FindObservable
, which you can
iterate to see the matching documents. Use the subscribe()
method to iterate
through the FindObservable
.
The following example uses the find()
method to find all documents in which
the founded_year
field has the value 1970
and prints the results:
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", ... }
Note
Find All Documents
To find all documents in a collection, call the find()
method
without passing any parameters:
collection.find()
Find One Document
To find a single document in a collection, call the find()
method
and pass a query filter that specifies the criteria of the document you want
to find. Then, chain the first()
method to find()
.
The find()
method returns a FindObservable
instance, and the first()
method returns a SingleObserver
instance that contains the first query result
stored by the FindObservable
. You can access the SingleObserver
result
by calling the subscribe()
method.
The following example uses the find()
and first()
methods to find the
first document in which the name
field has the value "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
Sort Order
The first()
method returns the first document in
natural order
on disk if no sort criteria is specified.
Modify Find Behavior
You can modify the behavior of the find()
method by chaining
methods provided by the FindObservable
class. The following
table describes some of these methods:
Method | Description |
---|---|
| 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 |
The following example uses the find()
method to find all documents in which
the number_of_employees
field has the value 1000
. The example uses the
limit()
method to return a maximum of 5
results:
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", ... }
For a full list of FindObservable
member methods, see the API documentation for the
FindObservable class.
Additional Information
To learn more about query filters, see the Specify a Query guide.
To view code examples that retrieve documents by using the Scala driver, see Read Data from MongoDB.
API Documentation
To learn more about any of the methods discussed in this guide, see the following API documentation: