Table of Contents

Method ForEachAsync

Namespace
MongoDB.Driver
Assembly
MongoDB.Driver.Core.dll

ForEachAsync<TDocument>(IAsyncCursor<TDocument>, Func<TDocument, Task>, CancellationToken)

Calls a delegate for each document returned by the cursor.

public static Task ForEachAsync<TDocument>(this IAsyncCursor<TDocument> source, Func<TDocument, Task> processor, CancellationToken cancellationToken = default)

Parameters

source IAsyncCursor<TDocument>

The source.

processor Func<TDocument, Task>

The processor.

cancellationToken CancellationToken

The cancellation token.

Returns

Task

A Task that completes when all the documents have been processed.

Type Parameters

TDocument

The type of the document.

ForEachAsync<TDocument>(IAsyncCursor<TDocument>, Func<TDocument, int, Task>, CancellationToken)

Calls a delegate for each document returned by the cursor.

public static Task ForEachAsync<TDocument>(this IAsyncCursor<TDocument> source, Func<TDocument, int, Task> processor, CancellationToken cancellationToken = default)

Parameters

source IAsyncCursor<TDocument>

The source.

processor Func<TDocument, int, Task>

The processor.

cancellationToken CancellationToken

The cancellation token.

Returns

Task

A Task that completes when all the documents have been processed.

Type Parameters

TDocument

The type of the document.

ForEachAsync<TDocument>(IAsyncCursor<TDocument>, Action<TDocument>, CancellationToken)

Calls a delegate for each document returned by the cursor.

public static Task ForEachAsync<TDocument>(this IAsyncCursor<TDocument> source, Action<TDocument> processor, CancellationToken cancellationToken = default)

Parameters

source IAsyncCursor<TDocument>

The source.

processor Action<TDocument>

The processor.

cancellationToken CancellationToken

The cancellation token.

Returns

Task

A Task that completes when all the documents have been processed.

Type Parameters

TDocument

The type of the document.

Remarks

If your delegate is going to take a long time to execute or is going to block consider using a different overload of ForEachAsync that uses a delegate that returns a Task instead.

ForEachAsync<TDocument>(IAsyncCursor<TDocument>, Action<TDocument, int>, CancellationToken)

Calls a delegate for each document returned by the cursor.

public static Task ForEachAsync<TDocument>(this IAsyncCursor<TDocument> source, Action<TDocument, int> processor, CancellationToken cancellationToken = default)

Parameters

source IAsyncCursor<TDocument>

The source.

processor Action<TDocument, int>

The processor.

cancellationToken CancellationToken

The cancellation token.

Returns

Task

A Task that completes when all the documents have been processed.

Type Parameters

TDocument

The type of the document.

Remarks

If your delegate is going to take a long time to execute or is going to block consider using a different overload of ForEachAsync that uses a delegate that returns a Task instead.