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
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
MongoDatabaseThe database that contains this collection.
name
stringThe name of the collection.
settings
MongoCollectionSettingsThe 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
IMongoQueryThe 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
query
IMongoQueryThe 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
BsonValueThe 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
GeoHaystackSearchArgsThe 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
x
doubleThe x coordinate of the starting location.
y
doubleThe y coordinate of the starting location.
options
IMongoGeoHaystackSearchOptionsThe 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
args
GeoNearArgsThe 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
query
IMongoQueryThe query (usually a QueryDocument or constructed using the Query builder).
x
doubleThe x coordinate of the starting location.
y
doubleThe y coordinate of the starting location.
limit
intThe 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
query
IMongoQueryThe query (usually a QueryDocument or constructed using the Query builder).
x
doubleThe x coordinate of the starting location.
y
doubleThe y coordinate of the starting location.
limit
intThe maximum number of results returned.
options
IMongoGeoNearOptionsOptions 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
document
TDefaultDocumentThe 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
TDefaultDocumentThe document to insert.
options
MongoInsertOptionsThe 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
TDefaultDocumentThe document to insert.
writeConcern
WriteConcernThe 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
MongoInsertOptionsThe 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
WriteConcernThe 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
TDefaultDocumentThe 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
TDefaultDocumentThe document to save.
options
MongoInsertOptionsThe 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
TDefaultDocumentThe document to save.
writeConcern
WriteConcernThe 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
ReadConcernThe 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
ReadPreferenceThe 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
WriteConcernThe write concern.
Returns
- MongoCollection<TDefaultDocument>
A new MongoCollection instance with a different write concern setting.