Docs Menu
Docs Home
/ / /
Kotlin Sync Driver
/

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 retrieve accurate and estimated counts of the number of documents in a collection.

The examples in this guide use the movies collection in the sample_mflix database from the Atlas sample datasets. To learn how to create a free MongoDB Atlas cluster and load the sample datasets, see the Get Started with Atlas guide.

The following Kotlin data class models the documents in this collection:

data class Movie(
@BsonId
val id: ObjectId,
val title: String
)

Use the countDocuments() method to count the number of documents that are in a collection. To count the number of documents that match specified search critera, pass a query filter to the countDocuments() method.

To learn more about specifying a query, see Specify a Query.

To return a count of all documents in the collection, call the countDocuments() method with no arguments, as shown in the following example:

println(collection.countDocuments())
21349

To return a count of documents that match specific search criteria, specify your query in the countDocuments() method. The following example prints a count of all documents in the movies collection that have a year field value equal to 1930:

println(collection.countDocuments(eq("year", "1930")))
10

The countDocuments() method accepts optional parameters in the form of a CountOptions object, which represents options you can use to configure the count operation. You can set these options by instantiating a new CountOptions object, setting the object's fields using the corresponding methods, and passing it to the countDocuments() method. If you don't specify any options, the driver does not customize the count operation.

The following table describes the options you can set to customize countDocuments():

Option
Description
comment
Specifies a comment to attach to the operation.
skip
Sets the number of documents to skip before returning results.
limit
Sets the maximum number of documents to count. Must be a positive integer.
maxTime
Sets the maximum amount of time to allow the operation to run, in milliseconds.
collation
Specifies the kind of language collation to use when sorting results. For more information, see Collation in the MongoDB Server manual.
hint
Sets the index to scan for documents.

The following example uses a CountOptions object to add a comment to the countDocuments() operation:

val options = CountOptions().comment("Retrieving count")
collection.countDocuments(Filters.empty(), options)

Use the estimatedDocumentCount() method to retrieve an estimate of the number of documents in a collection. The method estimates the amount of documents based on collection metadata, which can be faster than performing an accurate count.

The following example prints the estimated number of documents in a collection:

print(collection.estimatedDocumentCount())
21349

The estimatedDocumentCount() method accepts optional parameters in the form of an EstimatedDocumentCountOptions object, which represents options you can use to configure the count operation. You can set these options by instantiating a new EstimatedDocumentCountOptions object, setting the object's fields using the corresponding methods, and passing it to the estimatedDocumentCount() method. If you don't specify any options, the driver does not customize the count operation.

The following table describes the options you can set to customize estimatedDocumentCount():

Option
Description
comment
Specifies a comment to attach to the operation.
maxTime
Specifies the maximum amount of time to allow the operation to run, in milliseconds.

The following example uses an EstimatedDocumentCountOptions object to add a comment to the estimatedDocumentCount() operation:

val options = EstimatedDocumentCountOptions().comment("Retrieving count")
collection.estimatedDocumentCount(options)

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

Back

Specify Documents to Return