Method SingleOrDefault

| MongoDB CSharp Driver API Reference (original) (raw)

Namespace

MongoDB.Driver

Assembly

MongoDB.Driver.dll

SingleOrDefault(IAsyncCursor, CancellationToken)

Returns the only document of a cursor, or a default value if the cursor contains no documents. This method throws an exception if the cursor contains more than one document.

public static TDocument SingleOrDefault<TDocument>(this IAsyncCursor<TDocument> cursor, CancellationToken cancellationToken = default)

Parameters

cursor IAsyncCursor

The cursor.

cancellationToken CancellationToken

The cancellation token.

Returns

TDocument

The only document of a cursor, or a default value if the cursor contains no documents.

Type Parameters

TDocument

The type of the document.