Table of Contents

Class MongoCollection<TDefaultDocument>

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

Represents a MongoDB collection and the settings used to access it as well as a default document type. This class is thread-safe.

public class MongoCollection<TDefaultDocument> : MongoCollection

Type Parameters

TDefaultDocument

The default document type of the collection.

Inheritance
MongoCollection<TDefaultDocument>
Inherited Members
Extension Methods

Constructors

MongoCollection(MongoDatabase, string, MongoCollectionSettings)

Creates a new instance of MongoCollection. Normally you would call one of the indexers or GetCollection methods of MongoDatabase instead.

[Obsolete("Use database.GetCollection() instead.")]
public MongoCollection(MongoDatabase database, string name, MongoCollectionSettings settings)

Parameters

database MongoDatabase

The database that contains this collection.

name string

The name of the collection.

settings MongoCollectionSettings

The settings to use to access this collection.

Methods

Find(IMongoQuery)

Returns a cursor that can be used to find all documents in this collection that match the query as TDefaultDocuments.

public virtual MongoCursor<TDefaultDocument> Find(IMongoQuery query)

Parameters

query IMongoQuery

The query (usually a QueryDocument or constructed using the Query builder).

Returns

MongoCursor<TDefaultDocument>

A MongoCursor<TDocument>.

FindAll()

Returns a cursor that can be used to find all documents in this collection as TDefaultDocuments.

public virtual MongoCursor<TDefaultDocument> FindAll()

Returns

MongoCursor<TDefaultDocument>

A MongoCursor<TDocument>.

FindOne()

Returns one document in this collection as a TDefaultDocument.

public virtual TDefaultDocument FindOne()

Returns

TDefaultDocument

A TDefaultDocument (or null if not found).

FindOne(IMongoQuery)

Returns one document in this collection that matches a query as a TDefaultDocument.

public virtual TDefaultDocument FindOne(IMongoQuery query)

Parameters

query IMongoQuery

The query (usually a QueryDocument or constructed using the Query builder).

Returns

TDefaultDocument

A TDefaultDocument (or null if not found).

FindOneById(BsonValue)

Returns a cursor that can be used to find one document in this collection by its _id value as a TDefaultDocument.

public virtual TDefaultDocument FindOneById(BsonValue id)

Parameters

id BsonValue

The id of the document.

Returns

TDefaultDocument

A TDefaultDocument (or null if not found).

GeoHaystackSearch(GeoHaystackSearchArgs)

Runs a geoHaystack search command on this collection.

[Obsolete("GeoHaystack indexes were deprecated in server version 4.4.")]
public virtual GeoHaystackSearchResult<TDefaultDocument> GeoHaystackSearch(GeoHaystackSearchArgs args)

Parameters

args GeoHaystackSearchArgs

The args.

Returns

GeoHaystackSearchResult<TDefaultDocument>

A GeoHaystackSearchResult<TDocument>.

GeoHaystackSearch(double, double, IMongoGeoHaystackSearchOptions)

Runs a geoHaystack search command on this collection.

[Obsolete("Use the overload of GeoHaystackSearch that has a GeoHaystackSearchArgs parameter instead.")]
public virtual GeoHaystackSearchResult<TDefaultDocument> GeoHaystackSearch(double x, double y, IMongoGeoHaystackSearchOptions options)

Parameters

x double

The x coordinate of the starting location.

y double

The y coordinate of the starting location.

options IMongoGeoHaystackSearchOptions

The options for the geoHaystack search (null if none).

Returns

GeoHaystackSearchResult<TDefaultDocument>

A GeoHaystackSearchResult<TDocument>.

GeoNear(GeoNearArgs)

Runs a GeoNear command on this collection.

public virtual GeoNearResult<TDefaultDocument> GeoNear(GeoNearArgs args)

Parameters

args GeoNearArgs

The args.

Returns

GeoNearResult<TDefaultDocument>

A GeoNearResult<TDocument>.

GeoNear(IMongoQuery, double, double, int)

Runs a GeoNear command on this collection.

[Obsolete("Use the overload of GeoNear that takes a GeoNearArgs parameter instead.")]
public virtual GeoNearResult<TDefaultDocument> GeoNear(IMongoQuery query, double x, double y, int limit)

Parameters

query IMongoQuery

The query (usually a QueryDocument or constructed using the Query builder).

x double

The x coordinate of the starting location.

y double

The y coordinate of the starting location.

limit int

The maximum number of results returned.

Returns

GeoNearResult<TDefaultDocument>

A GeoNearResult<TDocument>.

GeoNear(IMongoQuery, double, double, int, IMongoGeoNearOptions)

Runs a GeoNear command on this collection.

[Obsolete("Use the overload of GeoNear that takes a GeoNearArgs parameter instead.")]
public virtual GeoNearResult<TDefaultDocument> GeoNear(IMongoQuery query, double x, double y, int limit, IMongoGeoNearOptions options)

Parameters

query IMongoQuery

The query (usually a QueryDocument or constructed using the Query builder).

x double

The x coordinate of the starting location.

y double

The y coordinate of the starting location.

limit int

The maximum number of results returned.

options IMongoGeoNearOptions

Options for the GeoNear command (see GeoNearOptionsDocument, GeoNearOptionsWrapper, and the GeoNearOptions builder).

Returns

GeoNearResult<TDefaultDocument>

A GeoNearResult<TDocument>.

InitializeOrderedBulkOperation()

Creates a fluent builder for an ordered bulk operation.

public virtual BulkWriteOperation<TDefaultDocument> InitializeOrderedBulkOperation()

Returns

BulkWriteOperation<TDefaultDocument>

A fluent bulk operation builder.

InitializeUnorderedBulkOperation()

Creates a fluent builder for an unordered bulk operation.

public virtual BulkWriteOperation<TDefaultDocument> InitializeUnorderedBulkOperation()

Returns

BulkWriteOperation<TDefaultDocument>

A fluent bulk operation builder.

Insert(TDefaultDocument)

Inserts a document into this collection (see also InsertBatch to insert multiple documents at once).

public virtual WriteConcernResult Insert(TDefaultDocument document)

Parameters

document TDefaultDocument

The document to insert.

Returns

WriteConcernResult

A WriteConcernResult (or null if WriteConcern is disabled).

Insert(TDefaultDocument, MongoInsertOptions)

Inserts a document into this collection (see also InsertBatch to insert multiple documents at once).

public virtual WriteConcernResult Insert(TDefaultDocument document, MongoInsertOptions options)

Parameters

document TDefaultDocument

The document to insert.

options MongoInsertOptions

The options to use for this Insert.

Returns

WriteConcernResult

A WriteConcernResult (or null if WriteConcern is disabled).

Insert(TDefaultDocument, WriteConcern)

Inserts a document into this collection (see also InsertBatch to insert multiple documents at once).

public virtual WriteConcernResult Insert(TDefaultDocument document, WriteConcern writeConcern)

Parameters

document TDefaultDocument

The document to insert.

writeConcern WriteConcern

The write concern to use for this Insert.

Returns

WriteConcernResult

A WriteConcernResult (or null if WriteConcern is disabled).

InsertBatch(IEnumerable<TDefaultDocument>)

Inserts multiple documents at once into this collection (see also Insert to insert a single document).

public virtual IEnumerable<WriteConcernResult> InsertBatch(IEnumerable<TDefaultDocument> documents)

Parameters

documents IEnumerable<TDefaultDocument>

The documents to insert.

Returns

IEnumerable<WriteConcernResult>

A list of WriteConcernResults (or null if WriteConcern is disabled).

InsertBatch(IEnumerable<TDefaultDocument>, MongoInsertOptions)

Inserts multiple documents at once into this collection (see also Insert to insert a single document).

public virtual IEnumerable<WriteConcernResult> InsertBatch(IEnumerable<TDefaultDocument> documents, MongoInsertOptions options)

Parameters

documents IEnumerable<TDefaultDocument>

The documents to insert.

options MongoInsertOptions

The options to use for this Insert.

Returns

IEnumerable<WriteConcernResult>

A list of WriteConcernResults (or null if WriteConcern is disabled).

InsertBatch(IEnumerable<TDefaultDocument>, WriteConcern)

Inserts multiple documents at once into this collection (see also Insert to insert a single document).

public virtual IEnumerable<WriteConcernResult> InsertBatch(IEnumerable<TDefaultDocument> documents, WriteConcern writeConcern)

Parameters

documents IEnumerable<TDefaultDocument>

The documents to insert.

writeConcern WriteConcern

The write concern to use for this Insert.

Returns

IEnumerable<WriteConcernResult>

A list of WriteConcernResults (or null if WriteConcern is disabled).

ParallelScan(ParallelScanArgs<TDefaultDocument>)

Scans an entire collection in parallel using multiple cursors.

[Obsolete("ParallelScan was deprecated in server version 4.1.")]
public virtual ReadOnlyCollection<IEnumerator<TDefaultDocument>> ParallelScan(ParallelScanArgs<TDefaultDocument> args)

Parameters

args ParallelScanArgs<TDefaultDocument>

The args.

Returns

ReadOnlyCollection<IEnumerator<TDefaultDocument>>

Multiple enumerators, one for each cursor.

Save(TDefaultDocument)

Saves a document to this collection. The document must have an identifiable Id field. Based on the value of the Id field Save will perform either an Insert or an Update.

public virtual WriteConcernResult Save(TDefaultDocument document)

Parameters

document TDefaultDocument

The document to save.

Returns

WriteConcernResult

A WriteConcernResult (or null if WriteConcern is disabled).

Save(TDefaultDocument, MongoInsertOptions)

Saves a document to this collection. The document must have an identifiable Id field. Based on the value of the Id field Save will perform either an Insert or an Update.

public virtual WriteConcernResult Save(TDefaultDocument document, MongoInsertOptions options)

Parameters

document TDefaultDocument

The document to save.

options MongoInsertOptions

The options to use for this Save.

Returns

WriteConcernResult

A WriteConcernResult (or null if WriteConcern is disabled).

Save(TDefaultDocument, WriteConcern)

Saves a document to this collection. The document must have an identifiable Id field. Based on the value of the Id field Save will perform either an Insert or an Update.

public virtual WriteConcernResult Save(TDefaultDocument document, WriteConcern writeConcern)

Parameters

document TDefaultDocument

The document to save.

writeConcern WriteConcern

The write concern to use for this Insert.

Returns

WriteConcernResult

A WriteConcernResult (or null if WriteConcern is disabled).

WithReadConcern(ReadConcern)

Returns a new MongoCollection instance with a different read concern setting.

public virtual MongoCollection<TDefaultDocument> WithReadConcern(ReadConcern readConcern)

Parameters

readConcern ReadConcern

The read concern.

Returns

MongoCollection<TDefaultDocument>

A new MongoCollection instance with a different read concern setting.

WithReadPreference(ReadPreference)

Returns a new MongoCollection instance with a different read preference setting.

public virtual MongoCollection<TDefaultDocument> WithReadPreference(ReadPreference readPreference)

Parameters

readPreference ReadPreference

The read preference.

Returns

MongoCollection<TDefaultDocument>

A new MongoCollection instance with a different read preference setting.

WithWriteConcern(WriteConcern)

Returns a new MongoCollection instance with a different write concern setting.

public virtual MongoCollection<TDefaultDocument> WithWriteConcern(WriteConcern writeConcern)

Parameters

writeConcern WriteConcern

The write concern.

Returns

MongoCollection<TDefaultDocument>

A new MongoCollection instance with a different write concern setting.