@ -25,6 +25,9 @@ using MongoDB.Bson.Serialization;
using MongoDB.Driver.Internal ;
namespace MongoDB.Driver {
/// <summary>
/// Represents a MongoDB server (either a single instance or a replica set).
/// </summary>
public class MongoServer {
#region private static fields
private static object staticLock = new object ( ) ;
@ -49,6 +52,11 @@ namespace MongoDB.Driver {
#endregion
#region constructors
/// <summary>
/// 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.
/// </summary>
/// <param name="settings">The settings for this instance of MongoServer.</param>
public MongoServer (
MongoServerSettings settings
) {
@ -61,16 +69,39 @@ namespace MongoDB.Driver {
#endregion
#region factory methods
/// <summary>
/// Creates a new instance or returns an existing instance of MongoServer. Only one instance
/// is created for each combination of server settings.
/// </summary>
/// <returns>
/// A new or an existing instance of MongoServer.
/// </returns>
public static MongoServer Create ( ) {
return Create ( "mongodb://localhost" ) ;
}
/// <summary>
/// Creates a new instance or returns an existing instance of MongoServer. Only one instance
/// is created for each combination of server settings.
/// </summary>
/// <param name="builder">Server settings in the form of a MongoConnectionStringBuilder.</param>
/// <returns>
/// A new or an existing instance of MongoServer.
/// </returns>
public static MongoServer Create (
MongoConnectionStringBuilder builder
) {
return Create ( builder . ToServerSettings ( ) ) ;
}
/// <summary>
/// Creates a new instance or returns an existing instance of MongoServer. Only one instance
/// is created for each combination of server settings.
/// </summary>
/// <param name="settings">Server settings.</param>
/// <returns>
/// A new or an existing instance of MongoServer.
/// </returns>
public static MongoServer Create (
MongoServerSettings settings
) {
@ -85,12 +116,28 @@ namespace MongoDB.Driver {
}
}
/// <summary>
/// Creates a new instance or returns an existing instance of MongoServer. Only one instance
/// is created for each combination of server settings.
/// </summary>
/// <param name="url">Server settings in the form of a MongoUrl.</param>
/// <returns>
/// A new or an existing instance of MongoServer.
/// </returns>
public static MongoServer Create (
MongoUrl url
) {
return Create ( url . ToServerSettings ( ) ) ;
}
/// <summary>
/// Creates a new instance or returns an existing instance of MongoServer. Only one instance
/// is created for each combination of server settings.
/// </summary>
/// <param name="connectionString">Server settings in the form of a connection string.</param>
/// <returns>
/// A new or an existing instance of MongoServer.
/// </returns>
public static MongoServer Create (
string connectionString
) {
@ -103,6 +150,14 @@ namespace MongoDB.Driver {
}
}
/// <summary>
/// Creates a new instance or returns an existing instance of MongoServer. Only one instance
/// is created for each combination of server settings.
/// </summary>
/// <param name="uri">Server settings in the form of a Uri.</param>
/// <returns>
/// A new or an existing instance of MongoServer.
/// </returns>
public static MongoServer Create (
Uri uri
) {
@ -112,30 +167,51 @@ namespace MongoDB.Driver {
#endregion
#region public properties
/// <summary>
/// Get the admin database for this server.
/// </summary>
public virtual MongoDatabase AdminDatabase {
get { return GetDatabase ( "admin" ) ; }
}
/// <summary>
/// Get the IP end points for this server.
/// </summary>
public virtual IEnumerable < IPEndPoint > EndPoints {
get { return endPoints ; }
}
/// <summary>
/// Get the index cache (used by EnsureIndex) for this server.
/// </summary>
public virtual IndexCache IndexCache {
get { return indexCache ; }
}
/// <summary>
/// Get the max document size for this server (not valid until connected).
/// </summary>
public virtual int MaxDocumentSize {
get { return maxDocumentSize ; }
}
/// <summary>
/// Get the max message length for this server (not valid until connected).
/// </summary>
public virtual int MaxMessageLength {
get { return maxMessageLength ; }
}
/// <summary>
/// Get a list of the members of the replica set (not valid until connected).
/// </summary>
public virtual IEnumerable < MongoServerAddress > ReplicaSet {
get { return replicaSet ; }
}
/// <summary>
/// Get the RequestStart nesting level for the current thread.
/// </summary>
public virtual int RequestNestingLevel {
get {
int threadId = Thread . CurrentThread . ManagedThreadId ;
@ -150,22 +226,41 @@ namespace MongoDB.Driver {
}
}
/// <summary>
/// Get the settings for this server.
/// </summary>
public virtual MongoServerSettings Settings {
get { return settings ; }
}
/// <summary>
/// Get the current state of this server.
/// </summary>
public virtual MongoServerState State {
get { return state ; }
}
#endregion
#region public indexers
/// <summary>
/// Get a MongoDatabase instance representing a database on this server. Only one instance
/// is created for each combination of database settings.
/// </summary>
/// <param name="databaseName">The name of the database.</param>
/// <returns>A new or existing instance of MongoDatabase.</returns>
public virtual MongoDatabase this [
string databaseName
] {
get { return GetDatabase ( databaseName ) ; }
}
/// <summary>
/// Get a MongoDatabase instance representing a database on this server. Only one instance
/// is created for each combination of database settings.
/// </summary>
/// <param name="databaseName">The name of the database.</param>
/// <param name="credentials">The credentials to use with this database.</param>
/// <returns>A new or existing instance of MongoDatabase.</returns>
public virtual MongoDatabase this [
string databaseName ,
MongoCredentials credentials
@ -173,12 +268,26 @@ namespace MongoDB.Driver {
get { return GetDatabase ( databaseName , credentials ) ; }
}
/// <summary>
/// Get a MongoDatabase instance representing a database on this server. Only one instance
/// is created for each combination of database settings.
/// </summary>
/// <param name="databaseSettings">The settings to use with this database.</param>
/// <returns>A new or existing instance of MongoDatabase.</returns>
public virtual MongoDatabase this [
MongoDatabaseSettings databaseSettings
] {
get { return GetDatabase ( databaseSettings ) ; }
}
/// <summary>
/// Get a MongoDatabase instance representing a database on this server. Only one instance
/// is created for each combination of database settings.
/// </summary>
/// <param name="databaseName">The name of the database.</param>
/// <param name="credentials">The credentials to use with this database.</param>
/// <param name="safeMode">The safe mode to use with this database.</param>
/// <returns>A new or existing instance of MongoDatabase.</returns>
public virtual MongoDatabase this [
string databaseName ,
MongoCredentials credentials ,
@ -187,6 +296,13 @@ namespace MongoDB.Driver {
get { return GetDatabase ( databaseName , credentials , safeMode ) ; }
}
/// <summary>
/// Get a MongoDatabase instance representing a database on this server. Only one instance
/// is created for each combination of database settings.
/// </summary>
/// <param name="databaseName">The name of the database.</param>
/// <param name="safeMode">The safe mode to use with this database.</param>
/// <returns>A new or existing instance of MongoDatabase.</returns>
public virtual MongoDatabase this [
string databaseName ,
SafeMode safeMode
@ -196,16 +312,29 @@ namespace MongoDB.Driver {
#endregion
#region public methods
/// <summary>
/// Clone a database.
/// </summary>
/// <param name="fromHost"></param>
public virtual void CloneDatabase (
string fromHost
) {
throw new NotImplementedException ( ) ;
}
/// <summary>
/// Connect to the server. Normally there is no need to call this method as
/// the driver will connect to the server automatically when needed.
/// </summary>
public virtual void Connect ( ) {
Connect ( settings . ConnectTimeout ) ;
}
/// <summary>
/// Connect to the server. Normally there is no need to call this method as
/// the driver will connect to the server automatically when needed.
/// </summary>
/// <param name="timeout">How long to wait before timing out.</param>
public virtual void Connect (
TimeSpan timeout
) {
@ -253,6 +382,11 @@ namespace MongoDB.Driver {
}
// TODO: fromHost parameter?
/// <summary>
/// Copy a database.
/// </summary>
/// <param name="from">The name of an existing database.</param>
/// <param name="to">The name of the new database.</param>
public virtual void CopyDatabase (
string from ,
string to
@ -260,12 +394,37 @@ namespace MongoDB.Driver {
throw new NotImplementedException ( ) ;
}
/// <summary>
/// Create an instance of MongoDatabaseSettings for the named database with the rest of the settings inherited. You can override some of these settings before calling GetDatabase.
/// </summary>
/// <param name="databaseName">The name of the database.</param>
/// <returns>An instance of MongoDatabase for <paramref name="databaseName"/>.</returns>
public virtual MongoDatabaseSettings CreateDatabaseSettings (
string databaseName
) {
return new MongoDatabaseSettings (
databaseName ,
settings . DefaultCredentials ,
settings . SafeMode ,
settings . SlaveOk
) ;
}
/// <summary>
/// Test whether a database exists.
/// </summary>
/// <param name="databaseName"></param>
/// <returns>True if the datbase exists.</returns>
public virtual bool DatabaseExists (
string databaseName
) {
return GetDatabaseNames ( ) . Contains ( databaseName ) ;
}
/// <summary>
/// Disconnect from the server. Normally there is no need to call this method so
/// you should be sure to have a good reason if you do call it.
/// </summary>
public virtual void Disconnect ( ) {
// normally called from a connection when there is a SocketException
// but anyone can call it if they want to close all sockets to the server
@ -284,6 +443,11 @@ namespace MongoDB.Driver {
}
}
/// <summary>
/// Drop a database.
/// </summary>
/// <param name="databaseName">The name of the database to be dropped.</param>
/// <returns>A <see cref="CommandResult"/>.</returns>
public virtual CommandResult DropDatabase (
string databaseName
) {
@ -292,12 +456,22 @@ namespace MongoDB.Driver {
return database . RunCommand ( command ) ;
}
/// <summary>
/// Fetch the document referred to by the DBRef.
/// </summary>
/// <param name="dbRef">The <see cref="MongoDBRef"/> to fetch.</param>
/// <returns>A BsonDocument (or null if the document was not found).</returns>
public virtual BsonDocument FetchDBRef (
MongoDBRef dbRef
) {
return FetchDBRefAs < BsonDocument > ( dbRef ) ;
}
/// <summary>
/// Fetch the document referred to by the DBRef, deserialized as a <typeparamref name="TDocument"/>.
/// </summary>
/// <param name="dbRef">The <see cref="MongoDBRef"/> to fetch.</param>
/// <returns>A <typeparamref name="TDocument"/> (or null if the document was not found).</returns>
public virtual TDocument FetchDBRefAs < TDocument > (
MongoDBRef dbRef
) {
@ -309,12 +483,25 @@ namespace MongoDB.Driver {
return database . FetchDBRefAs < TDocument > ( dbRef ) ;
}
/// <summary>
/// Get a MongoDatabase instance representing the admin database on this server. Only one instance
/// is created for each combination of database settings.
/// </summary>
/// <param name="credentials">The credentials to use with the admin database.</param>
/// <returns>A new or existing instance of MongoDatabase.</returns>
public virtual MongoDatabase GetAdminDatabase (
MongoCredentials credentials
) {
return GetDatabase ( "admin" , credentials ) ;
}
/// <summary>
/// Get a MongoDatabase instance representing the admin database on this server. Only one instance
/// is created for each combination of database settings.
/// </summary>
/// <param name="credentials">The credentials to use with the admin database.</param>
/// <param name="safeMode">The safe mode to use with the admin database.</param>
/// <returns>A new or existing instance of MongoDatabase.</returns>
public virtual MongoDatabase GetAdminDatabase (
MongoCredentials credentials ,
SafeMode safeMode
@ -322,12 +509,24 @@ namespace MongoDB.Driver {
return GetDatabase ( "admin" , credentials , safeMode ) ;
}
/// <summary>
/// Get a MongoDatabase instance representing the admin database on this server. Only one instance
/// is created for each combination of database settings.
/// </summary>
/// <param name="safeMode">The safe mode to use with the admin database.</param>
/// <returns>A new or existing instance of MongoDatabase.</returns>
public virtual MongoDatabase GetAdminDatabase (
SafeMode safeMode
) {
return GetDatabase ( "admin" , safeMode ) ;
}
/// <summary>
/// Get a MongoDatabase instance representing a database on this server. Only one instance
/// is created for each combination of database settings.
/// </summary>
/// <param name="databaseSettings">The settings to use with this database.</param>
/// <returns>A new or existing instance of MongoDatabase.</returns>
public virtual MongoDatabase GetDatabase (
MongoDatabaseSettings databaseSettings
) {
@ -342,42 +541,74 @@ namespace MongoDB.Driver {
}
}
/// <summary>
/// Get a MongoDatabase instance representing a database on this server. Only one instance
/// is created for each combination of database settings.
/// </summary>
/// <param name="databaseName">The name of the database.</param>
/// <returns>A new or existing instance of MongoDatabase.</returns>
public virtual MongoDatabase GetDatabase (
string databaseName
) {
var databaseSettings = GetDatabaseSettings ( databaseName ) ;
var databaseSettings = Create DatabaseSettings( databaseName ) ;
return GetDatabase ( databaseSettings ) ;
}
/// <summary>
/// Get a MongoDatabase instance representing a database on this server. Only one instance
/// is created for each combination of database settings.
/// </summary>
/// <param name="databaseName">The name of the database.</param>
/// <param name="credentials">The credentials to use with this database.</param>
/// <returns>A new or existing instance of MongoDatabase.</returns>
public virtual MongoDatabase GetDatabase (
string databaseName ,
MongoCredentials credentials
) {
var databaseSettings = GetDatabaseSettings ( databaseName ) ;
var databaseSettings = Create DatabaseSettings( databaseName ) ;
databaseSettings . Credentials = credentials ;
return GetDatabase ( databaseSettings ) ;
}
/// <summary>
/// Get a MongoDatabase instance representing a database on this server. Only one instance
/// is created for each combination of database settings.
/// </summary>
/// <param name="databaseName">The name of the database.</param>
/// <param name="credentials">The credentials to use with this database.</param>
/// <param name="safeMode">The safe mode to use with this database.</param>
/// <returns>A new or existing instance of MongoDatabase.</returns>
public virtual MongoDatabase GetDatabase (
string databaseName ,
MongoCredentials credentials ,
SafeMode safeMode
) {
var databaseSettings = GetDatabaseSettings ( databaseName ) ;
var databaseSettings = Create DatabaseSettings( databaseName ) ;
databaseSettings . Credentials = credentials ;
databaseSettings . SafeMode = safeMode ;
return GetDatabase ( databaseSettings ) ;
}
/// <summary>
/// Get a MongoDatabase instance representing a database on this server. Only one instance
/// is created for each combination of database settings.
/// </summary>
/// <param name="databaseName">The name of the database.</param>
/// <param name="safeMode">The safe mode to use with this database.</param>
/// <returns>A new or existing instance of MongoDatabase.</returns>
public virtual MongoDatabase GetDatabase (
string databaseName ,
SafeMode safeMode
) {
var databaseSettings = GetDatabaseSettings ( databaseName ) ;
var databaseSettings = Create DatabaseSettings( databaseName ) ;
databaseSettings . SafeMode = safeMode ;
return GetDatabase ( databaseSettings ) ;
}
/// <summary>
/// Get the names of the databases on this server.
/// </summary>
/// <returns>A list of database names.</returns>
public virtual IEnumerable < string > GetDatabaseNames ( ) {
var result = AdminDatabase . RunCommand ( "listDatabases" ) ;
var databaseNames = new List < string > ( ) ;
@ -389,17 +620,10 @@ namespace MongoDB.Driver {
return databaseNames ;
}
public virtual MongoDatabaseSettings GetDatabaseSettings (
string databaseName
) {
return new MongoDatabaseSettings (
databaseName ,
settings . DefaultCredentials ,
settings . SafeMode ,
settings . SlaveOk
) ;
}
/// <summary>
/// Get the last error (if any) that occurred on this connection. You MUST be within a RequestStart to call this method.
/// </summary>
/// <returns>The last error (<see cref=" GetLastErrorResult"/>)</returns>
public virtual GetLastErrorResult GetLastError ( ) {
if ( RequestNestingLevel = = 0 ) {
throw new InvalidOperationException ( "GetLastError can only be called if RequestStart has been called first" ) ;
@ -408,6 +632,11 @@ namespace MongoDB.Driver {
return adminDatabase . RunCommandAs < GetLastErrorResult > ( "getlasterror" ) ; // use all lowercase for backward compatibility
}
/// <summary>
/// Reconnect 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.
/// </summary>
public virtual void Reconnect ( ) {
lock ( serverLock ) {
Disconnect ( ) ;
@ -415,6 +644,10 @@ namespace MongoDB.Driver {
}
}
/// <summary>
/// Let the server know that this thread is done with a series of related operations. Instead of calling this method it is better
/// to put the return value of RequestStart in a using statement.
/// </summary>
public virtual void RequestDone ( ) {
int threadId = Thread . CurrentThread . ManagedThreadId ;
MongoConnection connection = null ;
@ -436,8 +669,13 @@ namespace MongoDB.Driver {
}
}
// the result of RequestStart is IDisposable so you can use RequestStart in a using statment
// and then RequestDone will be called automatically when leaving the using statement
/// <summary>
/// Let the server know that this thread is about to begin a series of related operations that must all occur
/// on the same connection. The return value of this method implements IDisposable and can be placed in a
/// using statement (in which case RequestDone will be called automatically when leaving the using statement).
/// </summary>
/// <param name="initialDatabase">One of the databases involved in the related operations.</param>
/// <returns>A helper object that implements IDisposable and calls <see cref="RequestDone"/> from the Dispose method.</returns>
public virtual IDisposable RequestStart (
MongoDatabase initialDatabase
) {
@ -460,28 +698,55 @@ namespace MongoDB.Driver {
}
}
/// <summary>
/// Remove all entries in the index cache used by EnsureIndex. Call this method
/// when you know (or suspect) that a process other than this one may have deleted one or
/// more indexes.
/// </summary>
public virtual void ResetIndexCache ( ) {
indexCache . Reset ( ) ;
}
/// <summary>
/// Run a command on the admin database.
/// </summary>
/// <param name="command">The command to run.</param>
/// <returns>The result of the command (see <see cref="CommandResult"/>).</returns>
public virtual CommandResult RunAdminCommand (
IMongoCommand command
) {
return RunAdminCommandAs < CommandResult > ( command ) ;
}
/// <summary>
/// Run a command on the admin database.
/// </summary>
/// <param name="commandName">The name of the command to run.</param>
/// <returns>The result of the command (as a <see cref="CommandResult"/>).</returns>
public virtual CommandResult RunAdminCommand (
string commandName
) {
return RunAdminCommandAs < CommandResult > ( commandName ) ;
}
/// <summary>
/// Run a command on the admin database.
/// </summary>
/// <typeparam name="TCommandResult">The type to use for the command result.</typeparam>
/// <param name="command">The command to run.</param>
/// <returns>The result of the command (as a <typeparamref name="TCommandResult"/>).</returns>
public virtual TCommandResult RunAdminCommandAs < TCommandResult > (
IMongoCommand command
) where TCommandResult : CommandResult , new ( ) {
return AdminDatabase . RunCommandAs < TCommandResult > ( command ) ;
}
/// <summary>
/// Run a command on the admin database.
/// </summary>
/// <typeparam name="TCommandResult">The type to use for the command result.</typeparam>
/// <param name="commandName">The name of the command to run.</param>
/// <returns>The result of the command (as a <typeparamref name="TCommandResult"/>).</returns>
public virtual TCommandResult RunAdminCommandAs < TCommandResult > (
string commandName
) where TCommandResult : CommandResult , new ( ) {