Class MongoDatabase
Represents a MongoDB database and the settings used to access it. This class is thread-safe.
public class MongoDatabase
- Inheritance
-
MongoDatabase
- Inherited Members
- Extension Methods
Constructors
MongoDatabase(MongoServer, string, MongoDatabaseSettings)
Creates a new instance of MongoDatabase. Normally you would call one of the indexers or GetDatabase methods of MongoServer instead.
[Obsolete("User server.GetDatabase() instead.")]
public MongoDatabase(MongoServer server, string name, MongoDatabaseSettings settings)
Parameters
serverMongoServerThe server that contains this database.
namestringThe name of the database.
settingsMongoDatabaseSettingsThe settings to use to access this database.
Properties
CommandCollection
Gets the command collection for this database.
[Obsolete("CommandCollection will be removed and there will be no replacement.")]
public virtual MongoCollection<BsonDocument> CommandCollection { get; }
Property Value
GridFS
Gets the default GridFS instance for this database. The default GridFS instance uses default GridFS settings. See also GetGridFS if you need to use GridFS with custom settings.
public virtual MongoGridFS GridFS { get; }
Property Value
this[string]
Gets a MongoCollection instance representing a collection on this database with a default document type of BsonDocument.
[Obsolete("Use GetCollection instead.")]
public virtual MongoCollection<BsonDocument> this[string collectionName] { get; }
Parameters
collectionNamestringThe name of the collection.
Property Value
- MongoCollection<BsonDocument>
An instance of MongoCollection.
this[string, WriteConcern]
Gets a MongoCollection instance representing a collection on this database with a default document type of BsonDocument.
[Obsolete("Use GetCollection instead.")]
public virtual MongoCollection<BsonDocument> this[string collectionName, WriteConcern writeConcern] { get; }
Parameters
collectionNamestringThe name of the collection.
writeConcernWriteConcernThe write concern to use when accessing this collection.
Property Value
- MongoCollection<BsonDocument>
An instance of MongoCollection.
Name
Gets the name of this database.
public virtual string Name { get; }
Property Value
Server
Gets the server that contains this database.
public virtual MongoServer Server { get; }
Property Value
Settings
Gets the settings being used to access this database.
public virtual MongoDatabaseSettings Settings { get; }
Property Value
Methods
AddUser(MongoUser)
Adds a user to this database.
[Obsolete("Use the new user management command 'createUser' or 'updateUser'.")]
public virtual void AddUser(MongoUser user)
Parameters
userMongoUserThe user.
CollectionExists(string)
Tests whether a collection exists on this database.
public virtual bool CollectionExists(string collectionName)
Parameters
collectionNamestringThe name of the collection.
Returns
- bool
True if the collection exists.
CreateCollection(string)
Creates a collection. MongoDB creates collections automatically when they are first used, so this command is mainly here for frameworks.
public virtual CommandResult CreateCollection(string collectionName)
Parameters
collectionNamestringThe name of the collection.
Returns
- CommandResult
A CommandResult.
CreateCollection(string, IMongoCollectionOptions)
Creates a collection. MongoDB creates collections automatically when they are first used, so you only need to call this method if you want to provide non-default options.
public virtual CommandResult CreateCollection(string collectionName, IMongoCollectionOptions options)
Parameters
collectionNamestringThe name of the collection.
optionsIMongoCollectionOptionsOptions for creating this collection (usually a CollectionOptionsDocument or constructed using the CollectionOptions builder).
Returns
- CommandResult
A CommandResult.
CreateView(string, string, IEnumerable<BsonDocument>, IMongoCreateViewOptions)
Creates a view.
public virtual CommandResult CreateView(string viewName, string viewOn, IEnumerable<BsonDocument> pipeline, IMongoCreateViewOptions options)
Parameters
viewNamestringThe name of the view.
viewOnstringThe name of the collection that the view is on.
pipelineIEnumerable<BsonDocument>The pipeline.
optionsIMongoCreateViewOptionsThe options.
Returns
- CommandResult
A CommandResult.
Drop()
Drops a database.
public virtual void Drop()
DropCollection(string)
Drops a collection.
public virtual CommandResult DropCollection(string collectionName)
Parameters
collectionNamestringThe name of the collection to drop.
Returns
- CommandResult
A CommandResult.
Eval(BsonJavaScript, params object[])
Evaluates JavaScript code at the server.
[Obsolete("Use the overload of Eval that has an EvalArgs parameter instead.")]
public virtual BsonValue Eval(BsonJavaScript code, params object[] args)
Parameters
codeBsonJavaScriptThe code to evaluate.
argsobject[]Optional arguments (only used when the code is a function with parameters).
Returns
- BsonValue
The result of evaluating the code.
Eval(EvalArgs)
Evaluates JavaScript code at the server.
public virtual BsonValue Eval(EvalArgs args)
Parameters
argsEvalArgsThe args.
Returns
- BsonValue
The result of evaluating the code.
Eval(EvalFlags, BsonJavaScript, params object[])
Evaluates JavaScript code at the server.
[Obsolete("Use the overload of Eval that has an EvalArgs parameter instead.")]
public virtual BsonValue Eval(EvalFlags flags, BsonJavaScript code, params object[] args)
Parameters
flagsEvalFlagsFlags that control Eval options.
codeBsonJavaScriptThe code to evaluate.
argsobject[]Optional arguments (only used when the code is a function with parameters).
Returns
- BsonValue
The result of evaluating the code.
FetchDBRef(MongoDBRef)
Fetches the document referred to by the DBRef.
public virtual BsonDocument FetchDBRef(MongoDBRef dbRef)
Parameters
dbRefMongoDBRefThe MongoDBRef to fetch.
Returns
- BsonDocument
A BsonDocument (or null if the document was not found).
FetchDBRefAs(Type, MongoDBRef)
Fetches the document referred to by the DBRef.
public virtual object FetchDBRefAs(Type documentType, MongoDBRef dbRef)
Parameters
documentTypeTypeThe nominal type of the document to fetch.
dbRefMongoDBRefThe MongoDBRef to fetch.
Returns
- object
An instance of nominalType (or null if the document was not found).
FetchDBRefAs<TDocument>(MongoDBRef)
Fetches the document referred to by the DBRef, deserialized as a TDocument.
public virtual TDocument FetchDBRefAs<TDocument>(MongoDBRef dbRef)
Parameters
dbRefMongoDBRefThe MongoDBRef to fetch.
Returns
- TDocument
A
TDocument(or null if the document was not found).
Type Parameters
TDocumentThe nominal type of the document to fetch.
FindAllUsers()
Finds all users of this database.
[Obsolete("Use the new user management command 'usersInfo'.")]
public virtual MongoUser[] FindAllUsers()
Returns
- MongoUser[]
An array of users.
FindUser(string)
Finds a user of this database.
[Obsolete("Use the new user management command 'usersInfo'.")]
public virtual MongoUser FindUser(string username)
Parameters
usernamestringThe username.
Returns
- MongoUser
The user.
GetCollection(string)
Gets a MongoCollection instance representing a collection on this database with a default document type of BsonDocument.
public virtual MongoCollection<BsonDocument> GetCollection(string collectionName)
Parameters
collectionNamestringThe name of the collection.
Returns
- MongoCollection<BsonDocument>
An instance of MongoCollection.
GetCollection(string, MongoCollectionSettings)
Gets a MongoCollection instance representing a collection on this database with a default document type of TDefaultDocument.
public virtual MongoCollection<BsonDocument> GetCollection(string collectionName, MongoCollectionSettings collectionSettings)
Parameters
collectionNamestringThe name of the collection.
collectionSettingsMongoCollectionSettingsThe settings to use when accessing this collection.
Returns
- MongoCollection<BsonDocument>
An instance of MongoCollection.
GetCollection(string, WriteConcern)
Gets a MongoCollection instance representing a collection on this database with a default document type of BsonDocument.
public virtual MongoCollection<BsonDocument> GetCollection(string collectionName, WriteConcern writeConcern)
Parameters
collectionNamestringThe name of the collection.
writeConcernWriteConcernThe write concern to use when accessing this collection.
Returns
- MongoCollection<BsonDocument>
An instance of MongoCollection.
GetCollection(Type, string)
Gets a MongoCollection instance representing a collection on this database with a default document type of BsonDocument.
public virtual MongoCollection GetCollection(Type defaultDocumentType, string collectionName)
Parameters
defaultDocumentTypeTypeThe default document type.
collectionNamestringThe name of the collection.
Returns
- MongoCollection
An instance of MongoCollection.
GetCollection(Type, string, MongoCollectionSettings)
Gets a MongoCollection instance representing a collection on this database with a default document type of BsonDocument.
public virtual MongoCollection GetCollection(Type defaultDocumentType, string collectionName, MongoCollectionSettings collectionSettings)
Parameters
defaultDocumentTypeTypeThe default document type.
collectionNamestringThe name of the collection.
collectionSettingsMongoCollectionSettingsThe settings to use when accessing this collection.
Returns
- MongoCollection
An instance of MongoCollection.
GetCollection(Type, string, WriteConcern)
Gets a MongoCollection instance representing a collection on this database with a default document type of BsonDocument.
public virtual MongoCollection GetCollection(Type defaultDocumentType, string collectionName, WriteConcern writeConcern)
Parameters
defaultDocumentTypeTypeThe default document type.
collectionNamestringThe name of the collection.
writeConcernWriteConcernThe write concern to use when accessing this collection.
Returns
- MongoCollection
An instance of MongoCollection.
GetCollectionNames()
Gets a list of the names of all the collections in this database.
public virtual IEnumerable<string> GetCollectionNames()
Returns
- IEnumerable<string>
A list of collection names.
GetCollection<TDefaultDocument>(string)
Gets a MongoCollection instance representing a collection on this database with a default document type of TDefaultDocument.
public virtual MongoCollection<TDefaultDocument> GetCollection<TDefaultDocument>(string collectionName)
Parameters
collectionNamestringThe name of the collection.
Returns
- MongoCollection<TDefaultDocument>
An instance of MongoCollection.
Type Parameters
TDefaultDocumentThe default document type for this collection.
GetCollection<TDefaultDocument>(string, MongoCollectionSettings)
Gets a MongoCollection instance representing a collection on this database with a default document type of TDefaultDocument.
public virtual MongoCollection<TDefaultDocument> GetCollection<TDefaultDocument>(string collectionName, MongoCollectionSettings collectionSettings)
Parameters
collectionNamestringThe name of the collection.
collectionSettingsMongoCollectionSettingsThe settings to use when accessing this collection.
Returns
- MongoCollection<TDefaultDocument>
An instance of MongoCollection.
Type Parameters
TDefaultDocumentThe default document type for this collection.
GetCollection<TDefaultDocument>(string, WriteConcern)
Gets a MongoCollection instance representing a collection on this database with a default document type of TDefaultDocument.
public virtual MongoCollection<TDefaultDocument> GetCollection<TDefaultDocument>(string collectionName, WriteConcern writeConcern)
Parameters
collectionNamestringThe name of the collection.
writeConcernWriteConcernThe write concern to use when accessing this collection.
Returns
- MongoCollection<TDefaultDocument>
An instance of MongoCollection.
Type Parameters
TDefaultDocumentThe default document type for this collection.
GetCurrentOp()
Gets the current operation.
public virtual BsonDocument GetCurrentOp()
Returns
- BsonDocument
The current operation.
GetGridFS(MongoGridFSSettings)
Gets an instance of MongoGridFS for this database using custom GridFS settings.
public virtual MongoGridFS GetGridFS(MongoGridFSSettings gridFSSettings)
Parameters
gridFSSettingsMongoGridFSSettingsThe GridFS settings to use.
Returns
- MongoGridFS
An instance of MongoGridFS.
GetProfilingInfo(IMongoQuery)
Gets one or more documents from the system.profile collection.
public MongoCursor<SystemProfileInfo> GetProfilingInfo(IMongoQuery query)
Parameters
queryIMongoQueryA query to select which documents to return.
Returns
- MongoCursor<SystemProfileInfo>
A cursor.
GetProfilingLevel()
Gets the current profiling level.
public GetProfilingLevelResult GetProfilingLevel()
Returns
- GetProfilingLevelResult
The profiling level.
GetSisterDatabase(string)
Gets a sister database on the same server.
public virtual MongoDatabase GetSisterDatabase(string databaseName)
Parameters
databaseNamestringThe name of the sister database.
Returns
- MongoDatabase
An instance of MongoDatabase.
GetStats()
Gets the current database stats.
public virtual DatabaseStatsResult GetStats()
Returns
- DatabaseStatsResult
An instance of DatabaseStatsResult.
IsCollectionNameValid(string, out string)
Checks whether a given collection name is valid in this database.
public virtual bool IsCollectionNameValid(string collectionName, out string message)
Parameters
collectionNamestringThe collection name.
messagestringAn error message if the collection name is not valid.
Returns
- bool
True if the collection name is valid; otherwise, false.
RemoveUser(MongoUser)
Removes a user from this database.
[Obsolete("Use RunCommand with a { dropUser: <username> } document.")]
public virtual void RemoveUser(MongoUser user)
Parameters
userMongoUserThe user to remove.
RemoveUser(string)
Removes a user from this database.
[Obsolete("Use RunCommand with a { dropUser: <username> } document.")]
public virtual void RemoveUser(string username)
Parameters
usernamestringThe username to remove.
RenameCollection(string, string)
Renames a collection on this database.
public virtual CommandResult RenameCollection(string oldCollectionName, string newCollectionName)
Parameters
oldCollectionNamestringThe old name for the collection.
newCollectionNamestringThe new name for the collection.
Returns
- CommandResult
A CommandResult.
RenameCollection(string, string, bool)
Renames a collection on this database.
public virtual CommandResult RenameCollection(string oldCollectionName, string newCollectionName, bool dropTarget)
Parameters
oldCollectionNamestringThe old name for the collection.
newCollectionNamestringThe new name for the collection.
dropTargetboolWhether to drop the target collection first if it already exists.
Returns
- CommandResult
A CommandResult.
RunCommand(IMongoCommand)
Runs a command on this database.
public virtual CommandResult RunCommand(IMongoCommand command)
Parameters
commandIMongoCommandThe command object.
Returns
- CommandResult
A CommandResult
RunCommand(string)
Runs a command on this database.
public virtual CommandResult RunCommand(string commandName)
Parameters
commandNamestringThe name of the command.
Returns
- CommandResult
A CommandResult
RunCommandAs(Type, IMongoCommand)
Runs a command on this database and returns the result as a TCommandResult.
public virtual CommandResult RunCommandAs(Type commandResultType, IMongoCommand command)
Parameters
commandResultTypeTypeThe command result type.
commandIMongoCommandThe command object.
Returns
- CommandResult
A TCommandResult
RunCommandAs(Type, string)
Runs a command on this database and returns the result as a TCommandResult.
public virtual CommandResult RunCommandAs(Type commandResultType, string commandName)
Parameters
Returns
- CommandResult
A TCommandResult
RunCommandAs<TCommandResult>(IMongoCommand)
Runs a command on this database and returns the result as a TCommandResult.
public virtual TCommandResult RunCommandAs<TCommandResult>(IMongoCommand command) where TCommandResult : CommandResult
Parameters
commandIMongoCommandThe command object.
Returns
- TCommandResult
A TCommandResult
Type Parameters
TCommandResultThe type of the returned command result.
RunCommandAs<TCommandResult>(IMongoCommand, ReadPreference)
Runs a command on this database and returns the result as a TCommandResult.
public TCommandResult RunCommandAs<TCommandResult>(IMongoCommand command, ReadPreference readPreference) where TCommandResult : CommandResult
Parameters
commandIMongoCommandThe command object.
readPreferenceReadPreferenceThe read preference.
Returns
- TCommandResult
A TCommandResult
Type Parameters
TCommandResultThe type of the returned command result.
RunCommandAs<TCommandResult>(string)
Runs a command on this database and returns the result as a TCommandResult.
public virtual TCommandResult RunCommandAs<TCommandResult>(string commandName) where TCommandResult : CommandResult
Parameters
commandNamestringThe name of the command.
Returns
- TCommandResult
A TCommandResult
Type Parameters
TCommandResultThe type of the returned command result.
SetProfilingLevel(ProfilingLevel)
Sets the level of profile information to write.
public virtual CommandResult SetProfilingLevel(ProfilingLevel level)
Parameters
levelProfilingLevelThe profiling level.
Returns
- CommandResult
A CommandResult.
SetProfilingLevel(ProfilingLevel, TimeSpan)
Sets the level of profile information to write.
public virtual CommandResult SetProfilingLevel(ProfilingLevel level, TimeSpan slow)
Parameters
levelProfilingLevelThe profiling level.
slowTimeSpanThe threshold that defines a slow query.
Returns
- CommandResult
A CommandResult.
ToString()
Gets a canonical string representation for this database.
public override string ToString()
Returns
- string
A canonical string representation for this database.
WithReadConcern(ReadConcern)
Returns a new MongoDatabase instance with a different read concern setting.
public virtual MongoDatabase WithReadConcern(ReadConcern readConcern)
Parameters
readConcernReadConcernThe read concern.
Returns
- MongoDatabase
A new MongoDatabase instance with a different read concern setting.
WithReadPreference(ReadPreference)
Returns a new MongoDatabase instance with a different read preference setting.
public virtual MongoDatabase WithReadPreference(ReadPreference readPreference)
Parameters
readPreferenceReadPreferenceThe read preference.
Returns
- MongoDatabase
A new MongoDatabase instance with a different read preference setting.
WithWriteConcern(WriteConcern)
Returns a new MongoDatabase instance with a different write concern setting.
public virtual MongoDatabase WithWriteConcern(WriteConcern writeConcern)
Parameters
writeConcernWriteConcernThe write concern.
Returns
- MongoDatabase
A new MongoDatabase instance with a different write concern setting.