Table of Contents

Method CountDocuments

Namespace
MongoDB.Driver
Assembly
MongoDB.Driver.dll

CountDocuments(FilterDefinition<TDocument>, CountOptions, CancellationToken)

Counts the number of documents in the collection. For a fast estimate of the total documents in a collection see EstimatedDocumentCount(EstimatedDocumentCountOptions, CancellationToken).

public virtual long CountDocuments(FilterDefinition<TDocument> filter, CountOptions options = null, CancellationToken cancellationToken = default)

Parameters

filter FilterDefinition<TDocument>

The filter.

options CountOptions

The options.

cancellationToken CancellationToken

The cancellation token.

Returns

long

The number of documents in the collection.

Remarks

Note: when migrating from Count to CountDocuments the following query operations must be replaced:

+-------------+--------------------------------+
| Operator    | Replacement                    |
+=============+================================+
| $where      |  $expr                         |
+-------------+--------------------------------+
| $near       |  $geoWithin with $center       |
+-------------+--------------------------------+
| $nearSphere |  $geoWithin with $centerSphere |
+-------------+--------------------------------+

CountDocuments(IClientSessionHandle, FilterDefinition<TDocument>, CountOptions, CancellationToken)

Counts the number of documents in the collection. For a fast estimate of the total documents in a collection see EstimatedDocumentCount(EstimatedDocumentCountOptions, CancellationToken).

public virtual long CountDocuments(IClientSessionHandle session, FilterDefinition<TDocument> filter, CountOptions options = null, CancellationToken cancellationToken = default)

Parameters

session IClientSessionHandle

The session.

filter FilterDefinition<TDocument>

The filter.

options CountOptions

The options.

cancellationToken CancellationToken

The cancellation token.

Returns

long

The number of documents in the collection.

Remarks

Note: when migrating from Count to CountDocuments the following query operations must be replaced:

+-------------+--------------------------------+
| Operator    | Replacement                    |
+=============+================================+
| $where      |  $expr                         |
+-------------+--------------------------------+
| $near       |  $geoWithin with $center       |
+-------------+--------------------------------+
| $nearSphere |  $geoWithin with $centerSphere |
+-------------+--------------------------------+