Table of Contents

Class MongoServer

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

Represents a MongoDB server (either a single instance or a replica set) and the settings used to access it. This class is thread-safe.

public class MongoServer
Inheritance
MongoServer
Inherited Members
Extension Methods

Constructors

MongoServer(MongoServerSettings)

Creates a new instance of MongoServer. Normally you will use one of the Create methods instead of the constructor to create instances of this class.

[Obsolete("Use client.GetServer instead.")]
public MongoServer(MongoServerSettings settings)

Parameters

settings MongoServerSettings

The settings for this instance of MongoServer.

Properties

Arbiters

Gets the arbiter instances.

public virtual MongoServerInstance[] Arbiters { get; }

Property Value

MongoServerInstance[]

Instance

Gets the one and only instance for this server.

public virtual MongoServerInstance Instance { get; }

Property Value

MongoServerInstance

Instances

Gets the instances for this server.

public virtual MongoServerInstance[] Instances { get; }

Property Value

MongoServerInstance[]

this[string]

Gets a MongoDatabase instance representing a database on this server.

[Obsolete("Use GetDatabase instead.")]
public virtual MongoDatabase this[string databaseName] { get; }

Parameters

databaseName string

The name of the database.

Property Value

MongoDatabase

A new or existing instance of MongoDatabase.

this[string, WriteConcern]

Gets a MongoDatabase instance representing a database on this server.

[Obsolete("Use GetDatabase instead.")]
public virtual MongoDatabase this[string databaseName, WriteConcern writeConcern] { get; }

Parameters

databaseName string

The name of the database.

writeConcern WriteConcern

The write concern to use with this database.

Property Value

MongoDatabase

A new or existing instance of MongoDatabase.

MaxServerCount

Gets or sets the maximum number of instances of MongoServer that will be allowed to be created.

public static int MaxServerCount { get; set; }

Property Value

int

MaxWireVersion

Gets the maximum wire version.

public int MaxWireVersion { get; }

Property Value

int

Passives

Gets the passive instances.

[Obsolete("Passives are treated the same as secondaries.")]
public virtual MongoServerInstance[] Passives { get; }

Property Value

MongoServerInstance[]

Primary

Gets the primary instance (null if there is no primary).

public virtual MongoServerInstance Primary { get; }

Property Value

MongoServerInstance

ReplicaSetName

Gets the name of the replica set (null if not connected to a replica set).

public virtual string ReplicaSetName { get; }

Property Value

string

Secondaries

Gets the secondary instances.

public virtual MongoServerInstance[] Secondaries { get; }

Property Value

MongoServerInstance[]

SequentialId

Gets the unique sequential Id for this server.

public virtual int SequentialId { get; }

Property Value

int

ServerCount

Gets the number of instances of MongoServer that have been created.

public static int ServerCount { get; }

Property Value

int

Settings

Gets the settings for this server.

public virtual MongoServerSettings Settings { get; }

Property Value

MongoServerSettings

State

Gets the current state of this server (as of the last operation, not updated until another operation is performed).

public virtual MongoServerState State { get; }

Property Value

MongoServerState

Methods

Connect()

Connects to the server. Normally there is no need to call this method as the driver will connect to the server automatically when needed.

public virtual void Connect()

Connect(TimeSpan)

Connects to the server. Normally there is no need to call this method as the driver will connect to the server automatically when needed.

public virtual void Connect(TimeSpan timeout)

Parameters

timeout TimeSpan

How long to wait before timing out.

DatabaseExists(string)

Tests whether a database exists.

public virtual bool DatabaseExists(string databaseName)

Parameters

databaseName string

The name of the database.

Returns

bool

True if the database exists.

Disconnect()

Disconnects from the server. Normally there is no need to call this method so you should be sure to have a good reason to call it.

public virtual void Disconnect()

DropDatabase(string)

Drops a database.

public virtual CommandResult DropDatabase(string databaseName)

Parameters

databaseName string

The name of the database to be dropped.

Returns

CommandResult

A CommandResult.

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

The document (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.

GetAllServers()

Gets an array containing a snapshot of the set of all servers that have been created so far.

public static MongoServer[] GetAllServers()

Returns

MongoServer[]

An array containing a snapshot of the set of all servers that have been created so far.

GetDatabase(string)

Gets a MongoDatabase instance representing a database on this server.

public virtual MongoDatabase GetDatabase(string databaseName)

Parameters

databaseName string

The name of the database.

Returns

MongoDatabase

A new or existing instance of MongoDatabase.

GetDatabase(string, MongoDatabaseSettings)

Gets a MongoDatabase instance representing a database on this server.

public virtual MongoDatabase GetDatabase(string databaseName, MongoDatabaseSettings databaseSettings)

Parameters

databaseName string

The name of the database.

databaseSettings MongoDatabaseSettings

The settings to use with this database.

Returns

MongoDatabase

A new or existing instance of MongoDatabase.

GetDatabase(string, WriteConcern)

Gets a MongoDatabase instance representing a database on this server.

public virtual MongoDatabase GetDatabase(string databaseName, WriteConcern writeConcern)

Parameters

databaseName string

The name of the database.

writeConcern WriteConcern

The write concern to use with this database.

Returns

MongoDatabase

A new or existing instance of MongoDatabase.

GetDatabaseNames()

Gets the names of the databases on this server.

public virtual IEnumerable<string> GetDatabaseNames()

Returns

IEnumerable<string>

A list of database names.

GetServerInstance(MongoServerAddress)

Gets the server instance.

public virtual MongoServerInstance GetServerInstance(MongoServerAddress address)

Parameters

address MongoServerAddress

The address.

Returns

MongoServerInstance

The server instance.

IsDatabaseNameValid(string, out string)

Checks whether a given database name is valid on this server.

public virtual bool IsDatabaseNameValid(string databaseName, out string message)

Parameters

databaseName string

The database name.

message string

An error message if the database name is not valid.

Returns

bool

True if the database name is valid; otherwise, false.

Ping()

Checks whether the server is alive (throws an exception if not). If server is a replica set, pings all members one at a time.

public virtual void Ping()

Reconnect()

Reconnects to the server. Normally there is no need to call this method. All connections are closed and new connections will be opened as needed. Calling this method frequently will result in connection thrashing.

public virtual void Reconnect()

UnregisterAllServers()

Unregisters all servers from the dictionary used by Create to remember which servers have already been created.

public static void UnregisterAllServers()

UnregisterServer(MongoServer)

Unregisters a server from the dictionary used by Create to remember which servers have already been created.

public static void UnregisterServer(MongoServer server)

Parameters

server MongoServer

The server to unregister.

WithReadConcern(ReadConcern)

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

public virtual MongoServer WithReadConcern(ReadConcern readConcern)

Parameters

readConcern ReadConcern

The read concern.

Returns

MongoServer

A new MongoServer instance with a different read concern setting.

WithReadPreference(ReadPreference)

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

public virtual MongoServer WithReadPreference(ReadPreference readPreference)

Parameters

readPreference ReadPreference

The read preference.

Returns

MongoServer

A new MongoServer instance with a different read preference setting.

WithWriteConcern(WriteConcern)

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

public virtual MongoServer WithWriteConcern(WriteConcern writeConcern)

Parameters

writeConcern WriteConcern

The write concern.

Returns

MongoServer

A new MongoServer instance with a different write concern setting.