Switch Operations to a Different Database



Common operation: Operations.ForDatabase

  • For reference, all common operations are listed here.

// Define default database on the store
var documentStore = new DocumentStore
{
    Urls = new[] { "yourServerURL" },
    Database = "DefaultDB"
}.Initialize();

using (documentStore)
{
    // Use 'ForDatabase', get operation executor for another database
    OperationExecutor opExecutor = documentStore.Operations.ForDatabase("AnotherDB");
    
    // Send the operation, e.g. 'GetRevisionsOperation' will be executed on "AnotherDB"
    var revisionsInAnotherDB = 
        opExecutor.Send(new GetRevisionsOperation<Order>("Orders/1-A"));
 
    // Without 'ForDatabase', the operation is executed "DefaultDB"
    var revisionsInDefaultDB =
        documentStore.Operations.Send(new GetRevisionsOperation<Company>("Company/1-A"));
}

Syntax:

OperationExecutor ForDatabase(string databaseName);
Parameters Type Description
databaseName string Name of the database to operate on
Return Value Description
OperationExecutor New instance of Operation Executor that is scoped to the requested database

Maintenance operation: Maintenance.ForDatabase

  • For reference, all maintenance operations are listed here.

// Define default database on the store
var documentStore = new DocumentStore
{
    Urls = new[] { "yourServerURL" },
    Database = "DefaultDB"
}.Initialize();

using (documentStore = new DocumentStore())
{
    // Use 'ForDatabase', get maintenance operation executor for another database
    MaintenanceOperationExecutor opExecutor = documentStore.Maintenance.ForDatabase("AnotherDB");
    
    // Send the maintenance operation, e.g. get database stats for "AnotherDB"
    var statsForAnotherDB =
        opExecutor.Send(new GetStatisticsOperation());
    
    // Without 'ForDatabase', the stats are retrieved for "DefaultDB"
    var statsForDefaultDB =
        documentStore.Maintenance.Send(new GetStatisticsOperation());
}

Syntax:

MaintenanceOperationExecutor ForDatabase(string databaseName);
Parameters Type Description
databaseName string Name of the database to operate on
Return Value Description
MaintenanceOperationExecutor New instance of Maintenance Operation Executor that is scoped to the requested database