Class MongoCollection<TDefaultDocument>
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
TDefaultDocumentThe 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
databaseMongoDatabaseThe database that contains this collection.
namestringThe name of the collection.
settingsMongoCollectionSettingsThe 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
queryIMongoQueryThe query (usually a QueryDocument or constructed using the Query builder).
Returns
- MongoCursor<TDefaultDocument>
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>
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
queryIMongoQueryThe 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
idBsonValueThe 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
argsGeoHaystackSearchArgsThe args.
Returns
- GeoHaystackSearchResult<TDefaultDocument>
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
xdoubleThe x coordinate of the starting location.
ydoubleThe y coordinate of the starting location.
optionsIMongoGeoHaystackSearchOptionsThe options for the geoHaystack search (null if none).
Returns
- GeoHaystackSearchResult<TDefaultDocument>
GeoNear(GeoNearArgs)
Runs a GeoNear command on this collection.
public virtual GeoNearResult<TDefaultDocument> GeoNear(GeoNearArgs args)
Parameters
argsGeoNearArgsThe args.
Returns
- GeoNearResult<TDefaultDocument>
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
queryIMongoQueryThe query (usually a QueryDocument or constructed using the Query builder).
xdoubleThe x coordinate of the starting location.
ydoubleThe y coordinate of the starting location.
limitintThe maximum number of results returned.
Returns
- GeoNearResult<TDefaultDocument>
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
queryIMongoQueryThe query (usually a QueryDocument or constructed using the Query builder).
xdoubleThe x coordinate of the starting location.
ydoubleThe y coordinate of the starting location.
limitintThe maximum number of results returned.
optionsIMongoGeoNearOptionsOptions for the GeoNear command (see GeoNearOptionsDocument, GeoNearOptionsWrapper, and the GeoNearOptions builder).
Returns
- GeoNearResult<TDefaultDocument>
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
documentTDefaultDocumentThe 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
documentTDefaultDocumentThe document to insert.
optionsMongoInsertOptionsThe 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
documentTDefaultDocumentThe document to insert.
writeConcernWriteConcernThe 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
documentsIEnumerable<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
documentsIEnumerable<TDefaultDocument>The documents to insert.
optionsMongoInsertOptionsThe 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
documentsIEnumerable<TDefaultDocument>The documents to insert.
writeConcernWriteConcernThe 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
argsParallelScanArgs<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
documentTDefaultDocumentThe 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
documentTDefaultDocumentThe document to save.
optionsMongoInsertOptionsThe 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
documentTDefaultDocumentThe document to save.
writeConcernWriteConcernThe 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
readConcernReadConcernThe 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
readPreferenceReadPreferenceThe 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
writeConcernWriteConcernThe write concern.
Returns
- MongoCollection<TDefaultDocument>
A new MongoCollection instance with a different write concern setting.