Count Documents
On this page
Overview
In this guide, you can learn how to use the Scala driver to retrieve an accurate and estimated count of the number of documents in a collection. The following methods count documents in a collection:
countDocuments()
: Returns the exact number of documents that match a query filter or that exist in a collectionestimatedDocumentCount()
: Returns the estimated number of documents in a collection
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.
Retrieve an Accurate Count
Use the countDocuments()
method to count the number of documents in a
collection. To count the number of documents that match specific search criteria,
pass a query filter to the countDocuments()
method.
To learn more about specifying a query, see the Specify a Query guide.
Count All Documents
To return a count of all documents in the collection, call the countDocuments()
method without passing any parameters, as shown in the following example:
collection.countDocuments() .subscribe((count: Long) => println(s"Number of documents: $count"), (e: Throwable) => println(s"There was an error: $e"))
Number of documents: 9500
Count Specific Documents
To return a count of documents that match specific search criteria, pass a query
filter to the countDocuments()
method.
The following example counts the number of documents in which the value of the
founded_year
field is 2010
:
collection.countDocuments(equal("founded_year", 2010)) .subscribe((count: Long) => println(s"Companies founded in 2010: $count"), (e: Throwable) => println(s"There was an error: $e"))
Number of companies founded in 2010: 33
Customize Count Behavior
You can modify the behavior of the countDocuments()
method by
passing a CountOptions
instance as a parameter. The following table
describes some member functions of the CountOptions
class, which you
can use to set options for the count operation:
Method | Description |
---|---|
| Sets the collation to use for the operation. Parameter Type: Collation |
| Sets the index to use for the operation. ParameterType: Bson |
| Sets the maximum number of documents to count. This value must be a positive integer. Parameter Type: int |
| Sets the maximum amount of time that the operation can run. Parameter Types: long and TimeUnit |
| Sets the number of documents to skip before counting documents. Parameter Type: int |
The following example uses the countDocuments()
method to count the number of
documents in which the number_of_employees
field has the value 50
and instructs the
operation to count a maximum of 100
results:
val countOptions = CountOptions().limit(100) collection.countDocuments(equal("number_of_employees", 50), countOptions) .subscribe((count: Long) => println(s"Companies with 50 employees: $count"), (e: Throwable) => println(s"There was an error: $e"))
Number of companies with 50 employees: 100
Retrieve an Estimated Count
You can retrieve an estimate of the number of documents in a collection by calling
the estimatedDocumentCount()
method. The method estimates the amount of documents
based on collection metadata, which might be faster than performing an accurate count.
The following example estimates the number of documents in a collection:
collection.estimatedDocumentCount() .subscribe((count: Long) => println(s"Estimated number of documents: $count"), (e: Throwable) => println(s"There was an error: $e"))
Estimated number of documents: 9500
Customize Estimated Count Behavior
You can modify the behavior of the estimatedDocumentCount()
method by
passing an EstimatedDocumentCountOptions
instance as a parameter. The
EstimatedDocumentCountOptions
class includes the maxTime()
member function,
which you can use to configure your EstimatedDocumentCountOptions
object and
set the maximum time that the count operation can run.
The following example uses the estimatedDocumentCount()
method to return an
estimate of the number of documents in the collection and sets a timeout of
3
seconds on the operation:
val estimatedOptions = EstimatedDocumentCountOptions().maxTime(3, SECONDS) collection.estimatedDocumentCount(estimatedOptions) .subscribe((count: Long) => println(s"Estimated number of documents: $count"), (e: Throwable) => println(s"There was an error: $e"))
Estimated number of documents: 9500
API Documentation
To learn more about any of the methods or types discussed in this guide, see the following API documentation: