Docs Menu
Docs Home
/ / /
C#/.NET
/ / /

Count Documents

On this page

  • Overview
  • Accurate Count
  • Estimated Count
  • Aggregation
  • Additional Information

In this guide, you can learn how to get an accurate and estimated count of the number of documents in your collection.

The examples in this guide use the following documents in a collection called students:

{ "_id": 1, "name": "Jonathon Howard ", "finalGrade": 87.5 }
{ "_id": 2, "name": "Keisha Freeman", "finalGrade": 12.3 }
{ "_id": 3, "name": "Wei Zhang", "finalGrade": 99.0 }
{ "_id": 4, "name": "Juan Gonzalez", "finalGrade": 85.5 }
{ "_id": 5, "name": "Erik Trout", "finalGrade": 72.3 }
{ "_id": 6, "name": "Demarcus Smith", "finalGrade": 88.8 }

The following Student class models the documents in this collection:

public class Student {
public int Id { get; set; }
public string Name { get; set; }
public double FinalGrade { get; set; }
}

Note

The documents in the students collection use the camel-case naming convention. The examples in this guide use a ConventionPack to deserialize the fields in the collection into Pascal case and map them to the properties in the Student class.

To learn more about custom serialization, see Custom Serialization.

To count the number of documents that match your query filter, use the CountDocuments() method. If you pass an empty query filter, this method returns the total number of documents in the collection.

The following example counts the number of documents where the value of finalGrade is less than 80:

var filter = Builders<Student>.Filter.Lt(s => s.FinalGrade, 80.0);
var count = _myColl.CountDocuments(filter);
Console.WriteLine("Number of documents with a final grade less than 80: " + count);
Number of documents with a final grade less than 80: 2

You can modify the behavior of CountDocuments() by passing a CountOptions type as a parameter. If you don't specify any options, the driver uses default values.

You can set the following properties in a CountOptions object:

Property
Description
Collation
The type of language collation to use when sorting results.
Default: null
Hint
The index to use to scan for documents to count.
Default: null
Limit
The maximum number of documents to count.
Default: 0
MaxTime
The maximum amount of time that the query can run on the server.
Default: null
Skip
The number of documents to skip before counting.
Default: 0

Tip

When you use CountDocuments() to return the total number of documents in a collection, MongoDB performs a collection scan. You can avoid a collection scan and improve the performance of this method by using a hint to take advantage of the built-in index on the _id field. Use this technique only when calling CountDocuments() with an empty query parameter.

var filter = Builders<Student>.Filter.Empty;
CountOptions opts = new CountOptions(){Hint = "_id_"};
var count = collection.CountDocuments(filter, opts);

To estimate the total number of documents in your collection, use the EstimatedDocumentCount() method.

Note

The EstimatedDocumentCount() method is more efficient than the CountDocuments() method because it uses the collection's metadata rather than scanning the entire collection.

You can modify the behavior of EstimatedDocumentCount() by passing a EstimatedDocumentCountOptions type as a parameter. If you don't specify any options, the driver uses default values.

You can set the following properties in a EstimatedDocumentCountOptions object:

Property
Description
MaxTime
The maximum amount of time that the query can run on the server.
Default: null

The following example estimates the number of documents in the students collection:

var count = _myColl.EstimatedDocumentCount();
Console.WriteLine("Estimated number of documents in the students collection: " + count);
Estimated number of documents in the students collection: 6

You can use the Count() builder method to count the number of documents in an aggregation pipeline.

The following example performs the following actions:

  • Specifies a match stage to find documents with a FinalGrade value greater than 80

  • Counts the number of documents that match the criteria

var filter = Builders<Student>
.Filter.Gt(s => s.FinalGrade, 80);
var result = _myColl.Aggregate().Match(filter).Count();
Console.WriteLine("Number of documents with a final grade more than 80: " + result.First().Count);
Number of documents with a final grade more than 80: 4

To learn more about the operations mentioned, see the following guides:

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

Back

Specify Fields to Return