Click or drag to resize

IMongoCollection<TDocument>.CountDocumentsAsync Method (IClientSessionHandle, FilterDefinition<TDocument>, CountOptions, CancellationToken)

Counts the number of documents in the collection.

Namespace:  MongoDB.Driver
Assembly:  MongoDB.Driver (in MongoDB.Driver.dll) Version: 2.8.0+cc573f3e1f48f39162b4b680e921a623e127e8fa
Syntax
Task<long> CountDocumentsAsync(
	IClientSessionHandle session,
	FilterDefinition<TDocument> filter,
	CountOptions options = null,
	CancellationToken cancellationToken = null
)

Parameters

session
Type: MongoDB.Driver.IClientSessionHandle
The session.
filter
Type: MongoDB.Driver.FilterDefinition<TDocument>
The filter.
options (Optional)
Type: MongoDB.Driver.CountOptions
The options.
cancellationToken (Optional)
Type: System.Threading.CancellationToken
The cancellation token.

Return Value

Type: Task<Int64>
The number of documents in the collection.
Remarks
Note: when migrating from CountAsync to CountDocumentsAsync the following query operations must be replaced:
+-------------+--------------------------------+
| Operator    | Replacement                    |
+=============+================================+
| $where      |  $expr                         |
+-------------+--------------------------------+
| $near       |  $geoWithin with $center       |
+-------------+--------------------------------+
| $nearSphere |  $geoWithin with $centerSphere |
+-------------+--------------------------------+
See Also