Note

It is imperative that the cursor get disposed once you are finished with it to ensure that resources on the server are cleaned up.

Some options are available in the optional FindOptions parameter such as setting maxTimeMS, a batch size, or a comment. Others are available as part of the fluent interface such as skip, limit, and sort.

Note

The order of the chained methods is unimportant. Limit followed by Skip is the same as Skip followed by Limit. In addition, specifying a method multiple times will result in the last one winning.

Iteration

Other methods of iteration besides using a cursor directly are available.

First, the ToList and ToListAsync methods are available. These methods are useful when the list will be small or you simply need them all as a list. If you are returning a large number of documents, then memory should be considered a factor.

var list = collection.Find(filter)
.Skip(10)
.ToList();

var list = await collection.Find(filter)
.Skip(10)
.ToListAsync();

Second, ForEachAsync is available. This method is useful when you just need to process each document and don’t need to keep them around.

Note

Stage Operators

All the stage operators are supported, however some of them must use the AppendStage method due to lack of support for certain projections in the language.

important

Unlike Find, the order that stages are defined in matters. Skip(10).Limit(20) is not the same as Limit(20).Skip(10).

$project

A $project is defined using the Project method and its overloads. Unlike in Find, an aggregate projection is not executed client-side and must be fully translatable to the server’s supported expressions. See expressions for more detail about the expressions available inside a $project.

Project(x => new { Name = x.FirstName + " " + x.LastName });

{ $project: { Name: { $concat: ['$FirstName', ' ', '$LastName'] } } }

Note

In an aggregation framework projection, a new type, either anonymous or named, must be used.

$match

A $match stage is defined using the Match method and its overloads. It follows the same requirements as that of Find.

Match(x => x.Age > 21);

{ $match: { Age: { $gt: 21 } } }

$redact

There is no method defined for a $redact stage. However, it can be added using AppendStage.

$limit

$skip

$unwind

An $unwind stage is defined using the Unwind method and its overloads. Because $unwind is a type of projection, you must provide a return type, although not specifying one will use the overload that projects into a BsonDocument.

Unwind(x => x.ArrayFieldToUnwind);

{ $unwind: 'ArrayFieldToUnwind' }

$group

A $group stage is defined using the Group method and its overloads. Because $unwind is a type of projection, you must provide a return type. The most useful of the overloads is where two lambda expressions are expressed, the first for the key and the second for the grouping. See expressions for more detail about the expressions available inside a $group.

As in project, it is required that the result of the grouping be a new type, either anonymous or named. If the Key of the grouping is not used, an _id will still be inserted as this is required by the $group operator.

$sort

A $sort stage is defined using the Sort method. However, SortBy, SortByDescending, ThenBy, and ThenByDescending are also available.

SortBy(x => x.LastName).ThenByDescending(x => x.Age);

{ $sort: { LastName: 1, Age: -1 } }

$geoNear

There is no method defined for a $geoNear stage. However, it can be added using AppendStage.

$out

A $out stage is defined using the Out or OutAsync methods. It must be the final stage and it triggers execution of the operation.