Table of Contents

Method SingleOrDefaultAsync

Namespace
MongoDB.Driver
Assembly
MongoDB.Driver.dll

SingleOrDefaultAsync<TDocument>(IAsyncCursorSource<TDocument>, CancellationToken)

Returns the only document of a cursor returned by a cursor source, 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 Task<TDocument> SingleOrDefaultAsync<TDocument>(this IAsyncCursorSource<TDocument> source, CancellationToken cancellationToken = default)

Parameters

source IAsyncCursorSource<TDocument>

The source.

cancellationToken CancellationToken

The cancellation token.

Returns

Task<TDocument>

A Task whose result is the only document of a cursor, or a default value if the cursor contains no documents.

Type Parameters

TDocument

The type of the document.