run · read

collections

storage.run({ action: "collections" })

Every collection in a document database, with how many documents each one holds.

document databasefreeno collection needed
Using Python?

Switch any code block to Python: the choice applies to every page. Python SDK guide and resource reference.

One of the two actions that work on the database rather than on a collection — so there is no collection to pass. The other is overview.

await storage.run({ action: "collections" });
// [{ name: "users", count: 1204 }, { name: "orders", count: 88311 }]
storage.run(action="collections")
# [{"name": "users", "count": 1204}, {"name": "orders", "count": 88311}]

The count is an estimate

Read from metadata, not by walking the documents

An exact count on a large collection means reading it. For a figure you will act on, use count with a filter; for « roughly how big is this », the estimate is what you want and it is free.

System collections are left out

Internal collections are filtered, so what comes back is what you put there. A collection you created and cannot see here is a sign you are connected to a different database — see the URI path on connect().

const collections = await storage.run({ action: "collections" });

if (!collections.length) {
  // Either it is empty, or the connection landed on the wrong database.
  // The database is read from the path of the URI: …/mydb
}
collections = storage.run(action="collections")

if not collections:
    # Either it is empty, or the connection landed on the wrong database.
    # The database is read from the path of the URI: …/mydb
    pass