Documents and collections

How databases, collections and documents relate, and what identifies and versions a document.

This page describes the three levels data is organized in, what a document's revision means, and how the server stores and loads documents.

Databases

A database groups collections and is the boundary for access. A user's grant, a database key's scope and a limit all name a database, and nothing granted on one database reaches another. See Authentication and access.

A database is known by an id, 32 lowercase hexadecimal characters, drawn by whoever creates it. The server keeps no name for it: what names it elsewhere (your configuration, a dashboard) maps to that id. An id is never used again once its database was removed.

Collections

A collection holds documents that share one schema. You create it with an id you draw (32 lowercase hexadecimal characters), a name and its first schema; the server stores the schema and checks every write against it.

A collection's schema is versioned. A new collection is at version 1, and each migration that is activated moves the collection and all its documents to the next version. The server keeps every version's schema.

A collection can only be removed once it holds no live document.

Documents

A document is one value shaped by its collection's schema, addressed by its database, collection and document id. Your application chooses the id: 1 to 128 characters of letters, digits and - . _ ~ : @.

Documents are independent. Each is consistent on its own, a transaction changes exactly one document, and there is no snapshot that spans several documents. Design a document around what people edit together: one board, one drawing, one form.

Revisions

Every committed change to a document advances its revision by one. Clients use it to tell where they are in the document's history, and a backend can use it to detect concurrent changes (see Transactions).

Creating a document starts an incarnation; deleting it ends the incarnation, and creating it again starts a new one. An edit made on one incarnation never applies to the next.

A document is also at a schema version: the collection version its value is currently shaped for.

Reading

There are two ways to read a document:

  • By id. A read of one document always reflects every write the server has answered.
  • By listing a collection. Listings are ordered by document id and paginated. A listing is not a snapshot: a document created or deleted while it runs may or may not appear in it. To read a document you just wrote, read it by id.

Storage and loading

Every committed change is written to PostgreSQL before it is acknowledged, and documents are checkpointed there too. A document is loaded into memory when a client or a request needs it, and kept there while it is in use. A document without sessions or work leaves memory after an idle period and is loaded again the next time it is needed, so a deployment is not limited by the number of documents it stores. Clients that keep a document open but quiet do not keep it in memory either; see Realtime sessions.

Next