Table of Contents

Class MongoDatabase

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

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

server MongoServer

The server that contains this database.

name string

The name of the database.

settings MongoDatabaseSettings

The 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

MongoCollection<BsonDocument>

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

MongoGridFS

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

collectionName string

The 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

collectionName string

The name of the collection.

writeConcern WriteConcern

The 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

string

Server

Gets the server that contains this database.

public virtual MongoServer Server { get; }

Property Value

MongoServer

Settings

Gets the settings being used to access this database.

public virtual MongoDatabaseSettings Settings { get; }

Property Value

MongoDatabaseSettings

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

user MongoUser

The user.

CollectionExists(string)

Tests whether a collection exists on this database.

public virtual bool CollectionExists(string collectionName)

Parameters

collectionName string

The 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

collectionName string

The 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

collectionName string

The name of the collection.

options IMongoCollectionOptions

Options 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

viewName string

The name of the view.

viewOn string

The name of the collection that the view is on.

pipeline IEnumerable<BsonDocument>

The pipeline.

options IMongoCreateViewOptions

The 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

collectionName string

The 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

code BsonJavaScript

The code to evaluate.

args object[]

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

args EvalArgs

The 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

flags EvalFlags

Flags that control Eval options.

code BsonJavaScript

The code to evaluate.

args object[]

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

dbRef MongoDBRef

The 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

documentType Type

The nominal type of the document to fetch.

dbRef MongoDBRef

The 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

dbRef MongoDBRef

The MongoDBRef to fetch.

Returns

TDocument

A TDocument (or null if the document was not found).

Type Parameters

TDocument

The 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

username string

The 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

collectionName string

The 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

collectionName string

The name of the collection.

collectionSettings MongoCollectionSettings

The 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

collectionName string

The name of the collection.

writeConcern WriteConcern

The 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

defaultDocumentType Type

The default document type.

collectionName string

The 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

defaultDocumentType Type

The default document type.

collectionName string

The name of the collection.

collectionSettings MongoCollectionSettings

The 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

defaultDocumentType Type

The default document type.

collectionName string

The name of the collection.

writeConcern WriteConcern

The 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

collectionName string

The name of the collection.

Returns

MongoCollection<TDefaultDocument>

An instance of MongoCollection.

Type Parameters

TDefaultDocument

The 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

collectionName string

The name of the collection.

collectionSettings MongoCollectionSettings

The settings to use when accessing this collection.

Returns

MongoCollection<TDefaultDocument>

An instance of MongoCollection.

Type Parameters

TDefaultDocument

The 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

collectionName string

The name of the collection.

writeConcern WriteConcern

The write concern to use when accessing this collection.

Returns

MongoCollection<TDefaultDocument>

An instance of MongoCollection.

Type Parameters

TDefaultDocument

The 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

gridFSSettings MongoGridFSSettings

The 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

query IMongoQuery

A 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

databaseName string

The 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

collectionName string

The collection name.

message string

An 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

user MongoUser

The 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

username string

The username to remove.

RenameCollection(string, string)

Renames a collection on this database.

public virtual CommandResult RenameCollection(string oldCollectionName, string newCollectionName)

Parameters

oldCollectionName string

The old name for the collection.

newCollectionName string

The 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

oldCollectionName string

The old name for the collection.

newCollectionName string

The new name for the collection.

dropTarget bool

Whether 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

command IMongoCommand

The command object.

Returns

CommandResult

A CommandResult

RunCommand(string)

Runs a command on this database.

public virtual CommandResult RunCommand(string commandName)

Parameters

commandName string

The 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

commandResultType Type

The command result type.

command IMongoCommand

The 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

commandResultType Type

The command result type.

commandName string

The name of the command.

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

command IMongoCommand

The command object.

Returns

TCommandResult

A TCommandResult

Type Parameters

TCommandResult

The 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

command IMongoCommand

The command object.

readPreference ReadPreference

The read preference.

Returns

TCommandResult

A TCommandResult

Type Parameters

TCommandResult

The 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

commandName string

The name of the command.

Returns

TCommandResult

A TCommandResult

Type Parameters

TCommandResult

The type of the returned command result.

SetProfilingLevel(ProfilingLevel)

Sets the level of profile information to write.

public virtual CommandResult SetProfilingLevel(ProfilingLevel level)

Parameters

level ProfilingLevel

The 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

level ProfilingLevel

The profiling level.

slow TimeSpan

The 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

readConcern ReadConcern

The 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

readPreference ReadPreference

The 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

writeConcern WriteConcern

The write concern.

Returns

MongoDatabase

A new MongoDatabase instance with a different write concern setting.