Switch Operations to a Different Database
-
By default, all operations work on the default database defined in the Document Store.
-
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: operations.for_database
- For reference, all common operations are listed here.
# Define default database on the store
document_store = DocumentStore(urls=["yourServerURL"], database="DefaultDB")
document_store.initialize()
with document_store:
# Use 'for_database', get operation executor for another database
op_executor = document_store.operations.for_database("AnotherDB")
# Send the operation, e.g. 'GetRevisionsOperation' will be executed on "AnotherDB"
revisions_in_another_db = op_executor.send(GetRevisionsOperation("Orders/1-A", Order))
# Without 'for_database', the operation is executed "DefaultDB"
revisions_in_default_db = document_store.operations.send(GetRevisionsOperation("Company/1-A", Company))
Syntax:
def for_database(self, database_name: str) -> OperationExecutor: ...
Parameters | Type | Description |
---|---|---|
database_name | str |
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.
# Define default database on the store
document_store = DocumentStore(urls=["yourServerURL"], database="DefaultDB")
document_store.initialize()
with DocumentStore() as document_store:
# Use 'for_database', get maintenance operation executor for another database
op_executor = document_store.maintenance.for_database("AnotherDB")
# Send the maintenance operation, e.g. get database stats for "AnotherDB"
stats_for_another_db = op_executor.send(GetStatisticsOperation())
# Without 'for_database', the stats are retrieved for "DefaultDB"
stats_for_default_db = document_store.maintenance.send(GetStatisticsOperation())
Syntax:
def for_database(self, database_name: str) -> MaintenanceOperationExecutor: ...
Parameters | Type | Description |
---|---|---|
database_name | str |
Name of the database to operate on |
Return Value | Description |
---|---|
MaintenanceOperationExecutor |
New instance of Maintenance Operation Executor that is scoped to the requested database |