Search code examples
pythonravendbravendb5

List RavenDB collections, using py-ravendb


I'm trying to query or list all collections in a RavenDB database, using RavenDB's Python Client.

So far I've come to something like:

URLS = ['http://localhost:8080']
DB_NAME = 'my-db'

store = DocumentStore(URLS, DB_NAME)
store.initialize()

with store.open_session() as session:
    collections = session.advanced.document_store.database_commands.get_collections(0, 50)

The last line errors out with:

AttributeError: 'DocumentStore' object has no attribute 'database_commands'

Obviously the database_commands isn't available. But how can I list all collections in a RavenDB v5.4+ database instead, using py-ravendb?


Solution

  • I think you can use the maintenance operation GetCollectionStatisticsOperation.
    It will give you class CollectionStatistics which has property collections.

    Operation definition is in:
    https://github.com/ravendb/ravendb-python-client/blob/b2eccad9e183c8988bb2bed808e5b3807c8a4081/ravendb/documents/operations/statistics.py#L63

    A sample code that calls a maintenance operation can be found here:
    https://github.com/ravendb/ravendb-python-client/blob/b2eccad9e183c8988bb2bed808e5b3807c8a4081/ravendb/tests/operations_tests/test_maintenance_operations.py#L19

    Note: that example demonstrates sending another operation,
    but... follow it and use something like:

    self.store.maintenance.send(GetCollectionStatisticsOperation())

    It should return you stats about the default database defined in your store.
    And one of the stats props is Collections