Docs Menu
Docs Home
/ / /
Scala
/

Count Documents

On this page

  • Overview
  • Sample Data
  • Retrieve an Accurate Count
  • Count All Documents
  • Count Specific Documents
  • Customize Count Behavior
  • Retrieve an Estimated Count
  • Customize Estimated Count Behavior
  • API Documentation

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 collection

  • estimatedDocumentCount(): Returns the estimated number of documents in a collection

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.

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.

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

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

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

collation()

Sets the collation to use for the operation.
Parameter Type: Collation

hint()

Sets the index to use for the operation.
ParameterType: Bson

limit()

Sets the maximum number of documents to count. This value must be a positive integer.
Parameter Type: int

maxTime()

Sets the maximum amount of time that the operation can run.
Parameter Types: long and TimeUnit

skip()

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

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

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

To learn more about any of the methods or types discussed in this guide, see the following API documentation:

Back

Distinct Field Values