Collection

Learn: Collection schema

We recommend you use FSL to create and update user-defined collections. See Collection schema.

You add data to Fauna as JSON-like documents, stored in collections.

Collection collection

Fauna stores user-defined collections as documents in the Collection system collection. These documents have the CollectionDef type and are an FQL definition of the FSL collection schema.

Collection documents have the following FQL structure:

{
  name: "Customer",
  coll: Collection,
  ts: Time("2099-10-03T20:45:53.780Z"),
  history_days: 0,
  fields: {
    name: {
      signature: "String"
    },
    email: {
      signature: "String"
    },
    address: {
      signature: "{ street: String, city: String, state: String, postalCode: String, country: String }"
    }
  },
  wildcard: "Any",
  computed_fields: {
    cart: {
      body: "(customer) => Order.byCustomerAndStatus(customer, \"cart\").first()",
      signature: "Order?"
    },
    orders: {
      body: "(customer) => Order.byCustomer(customer)",
      signature: "Set<Order>"
    }
  },
  indexes: {
    byEmail: {
      terms: [
        {
          field: ".email",
          mva: false
        }
      ],
      queryable: true,
      status: "complete"
    }
  },
  ttl_days: 10,
  constraints: [
    {
      unique: [
        {
          field: ".email",
          mva: false
        }
      ],
      status: "active"
    }
  ],
  document_ttls: true,
  migrations: [
    {
      add_wildcard: {}
    }
  ]
}
Field Type Read-only Description

name

Name of the collection.

coll

true

The Collection collection name.

ts

true

Timestamp of the most recent document write.

computed_fields

FQL definition of the collection schema’s computed fields. See Computed field definitions

constraints

FQL definition of the collection schema’s check and unique constraints. See Check constraint definitions and Unique constraint definitions.

data

Arbitrary user-defined metadata.

document_ttls

Boolean | null

If true, you can write to the ttl field in the collection’s documents.

document_ttls does not stop ttl-related deletions or affect ttl values set by the collection schema’s ttl_days field.

fields

FQL definition of the collection schema’s field definitions. See Field definitions .

history_days

Number of days of history to retain for collection documents. When the number of days lapses, document snapshots older than the interval are removed. Does not affect the current version of documents.

Defaults to 0 (Retain only current documents). See Document history.

indexes

FQL definition of the collection schema’s index definitions. See Index definitions.

indexes objects include fields you can use to monitor the index build:

Field Type Read-only Description

queryable

true

If true, the index is queryable. If false, the index is not queryable.

status

Union of pending, active, and failed.

true

Status of the index build. Possible values are pending, active, and failed.

migrations

FQL definition of the collection schema’s migrations block. See Migrations block .

ttl_days

Documents are deleted ttl_days number of days after their last write.

Defaults to null (Retained documents indefinitely).

wildcard

FQL definition of the collection schema’s wildcard constraint. See Wildcard constraints.

Static methods

You can use the following static methods to manage the Collection collection in FQL.

Method Description

Dynamically get the Collection by name.

Get the Set of all accessible Collection objects.

Get a Collection document by its name.

Create a Collection.

Get the first matching accessible Collection.

Get the name of the Collection collection as a string.

Get the Set of accessible Collections in the database.

Instance properties

Collection documents have the following properties.

Property Description

Get or update a collection’s definition, represented as a Collection document.

Instance methods

You can use the following instance methods to mange specific Collection documents in FQL.

Method Description

Get the Set of all documents in the Collection.

Get a document by its document ID.

Create a document in the Collection.

Create a document in the Collection with a data field.

Delete the Collection instance.

Test if a Collection exists.

Get the first matching accessible document in the Collection.

Get the Set of matching accessible documents in the Collection.

Get the Set of matching documents in the defined Collection index.

Is this article helpful? 

Tell Fauna how the article can be improved:
Visit Fauna's forums or email docs@fauna.com

Thank you for your feedback!