Table of Contents

Class MongoCursor

Namespace
MongoDB.Driver
Assembly
MongoDB.Driver.Legacy.dll

An object that can be enumerated to fetch the results of a query. The query is not sent to the server until you begin enumerating the results.

public abstract class MongoCursor : IEnumerable
Inheritance
MongoCursor
Implements
Derived
MongoCursor<TDocument>
Inherited Members
Extension Methods

Constructors

MongoCursor(MongoCollection, IMongoQuery, ReadConcern, ReadPreference, IBsonSerializer)

Initializes a new instance of the MongoCursor class.

protected MongoCursor(MongoCollection collection, IMongoQuery query, ReadConcern readConcern, ReadPreference readPreference, IBsonSerializer serializer)

Parameters

collection MongoCollection

The collection.

query IMongoQuery

The query.

readConcern ReadConcern

The read concern.

readPreference ReadPreference

The read preference.

serializer IBsonSerializer

The serializer.

MongoCursor(MongoCollection, IMongoQuery, ReadPreference, IBsonSerializer)

Creates a new MongoCursor. It is very unlikely that you will call this constructor. Instead, see all the Find methods in MongoCollection.

protected MongoCursor(MongoCollection collection, IMongoQuery query, ReadPreference readPreference, IBsonSerializer serializer)

Parameters

collection MongoCollection

The collection.

query IMongoQuery

The query.

readPreference ReadPreference

The read preference.

serializer IBsonSerializer

The serializer.

Properties

BatchSize

Gets or sets the batch size (the number of documents returned per batch).

public virtual int BatchSize { get; set; }

Property Value

int

Collation

Gets the collation.

public virtual Collation Collation { get; }

Property Value

Collation

Collection

Gets the collection that is being queried.

public virtual MongoCollection Collection { get; }

Property Value

MongoCollection

Database

Gets the database that constains the collection that is being queried.

public virtual MongoDatabase Database { get; }

Property Value

MongoDatabase

Fields

Gets or sets the fields that will be returned from the server.

public virtual IMongoFields Fields { get; set; }

Property Value

IMongoFields

Flags

Gets or sets the query flags.

public virtual QueryFlags Flags { get; set; }

Property Value

QueryFlags

IsFrozen

Gets whether the cursor has been frozen to prevent further changes.

public virtual bool IsFrozen { get; protected set; }

Property Value

bool

Limit

Gets or sets the limit on the number of documents to be returned.

public virtual int Limit { get; set; }

Property Value

int

MaxAwaitTime

Gets or sets the maximum await time for TailableAwait cursors.

public virtual TimeSpan? MaxAwaitTime { get; set; }

Property Value

TimeSpan?

The maximum await time for TailableAwait cursors.

Options

Gets or sets the cursor options. See also the individual Set{Option} methods, which are easier to use.

public virtual BsonDocument Options { get; set; }

Property Value

BsonDocument

Query

Gets the query that will be sent to the server.

public virtual IMongoQuery Query { get; }

Property Value

IMongoQuery

ReadConcern

Gets the read concern.

public virtual ReadConcern ReadConcern { get; }

Property Value

ReadConcern

ReadPreference

Gets or sets the read preference.

public virtual ReadPreference ReadPreference { get; set; }

Property Value

ReadPreference

Serializer

Gets the serializer.

public virtual IBsonSerializer Serializer { get; }

Property Value

IBsonSerializer

Server

Gets the server that the query will be sent to.

public virtual MongoServer Server { get; }

Property Value

MongoServer

Skip

Gets or sets the number of documents the server should skip before returning the rest of the documents.

public virtual int Skip { get; set; }

Property Value

int

Methods

Clone(Type)

Creates a clone of the cursor.

public virtual MongoCursor Clone(Type documentType)

Parameters

documentType Type

The type of the documents returned.

Returns

MongoCursor

A clone of the cursor.

Clone(Type, IBsonSerializer)

Creates a clone of the cursor.

public virtual MongoCursor Clone(Type documentType, IBsonSerializer serializer)

Parameters

documentType Type

The type of the documents returned.

serializer IBsonSerializer

The serializer to use.

Returns

MongoCursor

A clone of the cursor.

Clone<TDocument>()

Creates a clone of the cursor.

public virtual MongoCursor<TDocument> Clone<TDocument>()

Returns

MongoCursor<TDocument>

A clone of the cursor.

Type Parameters

TDocument

The type of the documents returned.

Clone<TDocument>(IBsonSerializer)

Creates a clone of the cursor.

public virtual MongoCursor<TDocument> Clone<TDocument>(IBsonSerializer serializer)

Parameters

serializer IBsonSerializer

The serializer to use.

Returns

MongoCursor<TDocument>

A clone of the cursor.

Type Parameters

TDocument

The type of the documents returned.

Count()

Returns the number of documents that match the query (ignores Skip and Limit, unlike Size which honors them).

public virtual long Count()

Returns

long

The number of documents that match the query.

Create(Type, MongoCollection, IMongoQuery, ReadConcern, ReadPreference, IBsonSerializer)

Creates a cursor.

[Obsolete("Use a method that returns a cursor instead.")]
public static MongoCursor Create(Type documentType, MongoCollection collection, IMongoQuery query, ReadConcern readConcern, ReadPreference readPreference, IBsonSerializer serializer)

Parameters

documentType Type

Type of the document.

collection MongoCollection

The collection.

query IMongoQuery

The query.

readConcern ReadConcern

The read concern.

readPreference ReadPreference

The read preference.

serializer IBsonSerializer

The serializer.

Returns

MongoCursor

A cursor.

Create(Type, MongoCollection, IMongoQuery, ReadPreference, IBsonSerializer)

Creates a cursor.

[Obsolete("Use a method that returns a cursor instead.")]
public static MongoCursor Create(Type documentType, MongoCollection collection, IMongoQuery query, ReadPreference readPreference, IBsonSerializer serializer)

Parameters

documentType Type

The type of the returned documents.

collection MongoCollection

The collection to query.

query IMongoQuery

A query.

readPreference ReadPreference

The read preference.

serializer IBsonSerializer

The serializer.

Returns

MongoCursor

A cursor.

CreateExplainOperation(ExplainVerbosity)

Creates an explain operation for this cursor.

protected abstract ExplainOperation CreateExplainOperation(ExplainVerbosity verbosity)

Parameters

verbosity ExplainVerbosity

The requested ExplainVerbosity.

Returns

ExplainOperation

An explain operation.

Explain()

Returns an explanation of how the query was executed (instead of the results).

public virtual BsonDocument Explain()

Returns

BsonDocument

An explanation of thow the query was executed.

Explain(bool)

Returns an explanation of how the query was executed (instead of the results).

public virtual BsonDocument Explain(bool verbose)

Parameters

verbose bool

Whether the explanation should contain more details.

Returns

BsonDocument

An explanation of throw the query was executed.

IEnumerableGetEnumerator()

Gets the non-generic enumerator.

protected abstract IEnumerator IEnumerableGetEnumerator()

Returns

IEnumerator

The enumerator.

SetBatchSize(int)

Sets the batch size (the number of documents returned per batch).

public virtual MongoCursor SetBatchSize(int batchSize)

Parameters

batchSize int

The number of documents in each batch.

Returns

MongoCursor

The cursor (so you can chain method calls to it).

SetCollation(Collation)

Sets the collation.

public virtual MongoCursor SetCollation(Collation collation)

Parameters

collation Collation

The collation.

Returns

MongoCursor

The cursor (so you can chain method calls to it).

SetFields(IMongoFields)

Sets the fields that will be returned from the server.

public virtual MongoCursor SetFields(IMongoFields fields)

Parameters

fields IMongoFields

The fields that will be returned from the server.

Returns

MongoCursor

The cursor (so you can chain method calls to it).

SetFields(params string[])

Sets the fields that will be returned from the server.

public virtual MongoCursor SetFields(params string[] fields)

Parameters

fields string[]

The fields that will be returned from the server.

Returns

MongoCursor

The cursor (so you can chain method calls to it).

SetFlags(QueryFlags)

Sets the query flags.

public virtual MongoCursor SetFlags(QueryFlags flags)

Parameters

flags QueryFlags

The query flags.

Returns

MongoCursor

The cursor (so you can chain method calls to it).

SetHint(BsonDocument)

Sets the index hint for the query.

public virtual MongoCursor SetHint(BsonDocument hint)

Parameters

hint BsonDocument

The index hint.

Returns

MongoCursor

The cursor (so you can chain method calls to it).

SetHint(string)

Sets the index hint for the query.

public virtual MongoCursor SetHint(string indexName)

Parameters

indexName string

The name of the index.

Returns

MongoCursor

The cursor (so you can chain method calls to it).

SetLimit(int)

Sets the limit on the number of documents to be returned.

public virtual MongoCursor SetLimit(int limit)

Parameters

limit int

The limit on the number of documents to be returned.

Returns

MongoCursor

The cursor (so you can chain method calls to it).

SetMax(BsonDocument)

Sets the max value for the index key range of documents to return (note: the max value itself is excluded from the range). Often combined with SetHint (if SetHint is not used the server will attempt to determine the matching index automatically).

public virtual MongoCursor SetMax(BsonDocument max)

Parameters

max BsonDocument

The max value.

Returns

MongoCursor

The cursor (so you can chain method calls to it).

SetMaxAwaitTime(TimeSpan?)

Sets the maximum await time for tailable await cursors.

public virtual MongoCursor SetMaxAwaitTime(TimeSpan? value)

Parameters

value TimeSpan?

The value.

Returns

MongoCursor

The cursor (so you can chain method calls to it).

SetMaxScan(int)

Sets the maximum number of documents to scan.

[Obsolete("MaxScan was deprecated in server version 4.0.")]
public virtual MongoCursor SetMaxScan(int maxScan)

Parameters

maxScan int

The maximum number of documents to scan.

Returns

MongoCursor

The cursor (so you can chain method calls to it).

SetMaxTime(TimeSpan)

Sets the maximum time the server should spend on this query.

public virtual MongoCursor SetMaxTime(TimeSpan maxTime)

Parameters

maxTime TimeSpan

The max time.

Returns

MongoCursor

The cursor (so you can chain method calls to it).

SetMin(BsonDocument)

Sets the min value for the index key range of documents to return (note: the min value itself is included in the range). Often combined with SetHint (if SetHint is not used the server will attempt to determine the matching index automatically).

public virtual MongoCursor SetMin(BsonDocument min)

Parameters

min BsonDocument

The min value.

Returns

MongoCursor

The cursor (so you can chain method calls to it).

SetOption(string, BsonValue)

Sets a cursor option.

public virtual MongoCursor SetOption(string name, BsonValue value)

Parameters

name string

The name of the option.

value BsonValue

The value of the option.

Returns

MongoCursor

The cursor (so you can chain method calls to it).

SetOptions(BsonDocument)

Sets multiple cursor options. See also the individual Set{Option} methods, which are easier to use.

public virtual MongoCursor SetOptions(BsonDocument options)

Parameters

options BsonDocument

The options.

Returns

MongoCursor

The cursor (so you can chain method calls to it).

SetReadPreference(ReadPreference)

Sets the read preference.

public virtual MongoCursor SetReadPreference(ReadPreference readPreference)

Parameters

readPreference ReadPreference

The read preference.

Returns

MongoCursor

The cursor (so you can chain method calls to it).

SetSerializer(IBsonSerializer)

Sets the serializer.

public virtual MongoCursor SetSerializer(IBsonSerializer serializer)

Parameters

serializer IBsonSerializer

The serializer.

Returns

MongoCursor

The cursor (so you can chain method calls to it).

SetShowDiskLoc()

Sets the $showDiskLoc option.

public virtual MongoCursor SetShowDiskLoc()

Returns

MongoCursor

The cursor (so you can chain method calls to it).

SetSkip(int)

Sets the number of documents the server should skip before returning the rest of the documents.

public virtual MongoCursor SetSkip(int skip)

Parameters

skip int

The number of documents to skip.

Returns

MongoCursor

The cursor (so you can chain method calls to it).

SetSnapshot()

Sets the $snapshot option.

[Obsolete("Snapshot was deprecated in server version 3.7.4.")]
public virtual MongoCursor SetSnapshot()

Returns

MongoCursor

The cursor (so you can chain method calls to it).

SetSortOrder(IMongoSortBy)

Sets the sort order for the server to sort the documents by before returning them.

public virtual MongoCursor SetSortOrder(IMongoSortBy sortBy)

Parameters

sortBy IMongoSortBy

The sort order.

Returns

MongoCursor

The cursor (so you can chain method calls to it).

SetSortOrder(params string[])

Sets the sort order for the server to sort the documents by before returning them.

public virtual MongoCursor SetSortOrder(params string[] keys)

Parameters

keys string[]

The names of the fields to sort by.

Returns

MongoCursor

The cursor (so you can chain method calls to it).

Size()

Returns the size of the result set (honors Skip and Limit, unlike Count which does not).

public virtual long Size()

Returns

long

The size of the result set.