---
title: "Switch Operations to a Different Database"
url: "https://docs.ravendb.net/6.2/client-api/operations/how-to/switch-operations-to-a-different-database"
version: "6.2"
language: "php"
---

# Switch Operations to a Different Database

> Client language: PHP. Also available for [C#](https://docs.ravendb.net/6.2/client-api/operations/how-to/switch-operations-to-a-different-database.md), [Java](https://docs.ravendb.net/6.2/client-api/operations/how-to/switch-operations-to-a-different-database.java.md), [Python](https://docs.ravendb.net/6.2/client-api/operations/how-to/switch-operations-to-a-different-database.python.md), [Node.js](https://docs.ravendb.net/6.2/client-api/operations/how-to/switch-operations-to-a-different-database.nodejs.md).
>
> RavenDB publishes an official agent skill for coding assistants at https://docs.ravendb.net/skills/ravendb/SKILL.md (also installable with `npx skills add ravendb/skills`). The skill describes itself as: "Read this before writing any RavenDB query, client call or /databases/ request. Covers server setup, document modelling, the Node.js, .NET and Python clients, RQL, indexes, vector search and AI agents. Code written from recall tends to run and return the wrong rows, because RQL resembles SQL and the clients take field names as plain strings."

> **Note**
>
> - By default, all operations work on the default database defined in the [Document Store](https://docs.ravendb.net/6.2/client-api/creating-document-store.md).
>
> - **To operate on a different database**, use the `for_database` method.  
>   If the requested database doesn't exist on the server, an exception will be thrown.
>
> - In this page:
>   - [Common operation: `forDatabase`](#common-operation:-fordatabase)
>   - [Maintenance operation: `maintenance.for_database`](#maintenance-operation:-maintenancefor_database)

## Common operation: `forDatabase`

- For reference, all common operations are listed [here](https://docs.ravendb.net/6.2/client-api/operations/what-are-operations.php.md#common-operations).

```php
// Define default database on the store
$documentStore = new DocumentStore(
  ["yourServerURL"],
  "DefaultDB"
);
$documentStore->initialize();

try {
  // Use 'ForDatabase', get operation executor for another database
  /** @var OperationExecutor $opExecutor */
  $opExecutor = $documentStore->operations()->forDatabase("AnotherDB");

  // Send the operation, e.g. 'GetRevisionsOperation' will be executed on "AnotherDB"
  $revisionsInAnotherDB = $opExecutor->send(new GetRevisionsOperation(Order::class, "Orders/1-A"));

  // Without 'ForDatabase', the operation is executed "DefaultDB"
  $revisionsInDefaultDB = $documentStore->operations()->send(new GetRevisionsOperation(Company::class, "Company/1-A"));
} finally {
  $documentStore->close();
}
```

**Syntax**:

```php
public function forDatabase(?string $databaseName): OperationExecutor;
```

| 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.for_database`

- For reference, all maintenance operations are listed [here](https://docs.ravendb.net/6.2/client-api/operations/what-are-operations.php.md#maintenance-operations).

```php
// Define default database on the store
$documentStore = new DocumentStore(
  [ "yourServerURL" ],
  "DefaultDB"
);
$documentStore->initialize();

try {
  // Use 'ForDatabase', get maintenance operation executor for another database
  /** @var MaintenanceOperationExecutor $opExecutor */
  $opExecutor = $documentStore->maintenance()->forDatabase("AnotherDB");

  // Send the maintenance operation, e.g. get database stats for "AnotherDB"
  $statsForAnotherDB = $opExecutor->send(new GetStatisticsOperation());

  // Without 'ForDatabase', the stats are retrieved for "DefaultDB"
  $statsForDefaultDB = $documentStore->maintenance()->send(new GetStatisticsOperation());
} finally {
  $documentStore->close();
}
```

**Syntax**:

```php
public function forDatabase(?string $databaseName): MaintenanceOperationExecutor;
```

| 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 |

## See also

- [What are Operations](https://docs.ravendb.net/6.2/client-api/operations/what-are-operations.php.md)
