Check out v4 of the Fauna CLI

v4 of the Fauna CLI is now in beta.

The new version introduces enhancements to the developer experience, including an improved authentication workflow. To get started, check out the CLI v4 quick start.

Collection

Learn: Collections

We recommend you use FSL to create and update user-defined collections. See FSL 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 version of the FSL collection schema.

Collection documents have the following FQL structure:

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

name

true

Name of the collection.

alias

Alias used to reference a collection with a name that conflicts with a reserved schema name. By creating and using an alias, the resource doesn’t have to be renamed. See Schema aliasing.

coll

true

Collection name: Collection.

ts

true

Last time the document was created or updated.

computed_fields

FQL version of the collection schema’s computed fields. See FSL collection schema: Computed field definitions

fields

FQL version of the collection schema’s field definitions. See FSL collection schema: Field definitions.

migrations

FQL version of the collection schema’s migrations block. See FSL collection schema: Migrations block.

wildcard

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

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.

ttl_days

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

Defaults to null (Retained documents indefinitely).

document_ttls

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.

indexes

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

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

constraints

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

data

{ *: Any } | Null

Arbitrary user-defined metadata for the document.

indexes build fields

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

Field Type Read-only Required 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.

Static methods

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

Method Description

Access a collection by its name.

Get a Set of all collection definitions.

Get a collection definitions by its name.

Create a collection.

Get the first collection definition that matches a provided predicate.

Get "Collection" as a String.

Get a Set of collection definitions that match a provided predicate.

Instance properties

Collection documents have the following properties. You access the property using an existing collection’s name.

Property Description

Get a collection definition, represented as a Collection document with the CollectionDef type.

Instance methods

You can use the following instance methods to manage collection definitions, represented as Collection documents, in FQL. You call the methods on a CollectionDef.

Method Description

Delete a collection.

Test if a collection exists.

Replaces a collection definition.

Collection name methods

You can use the following collection name methods to create and manage documents within a user-defined collection using FQL. You call the methods directly on an existing collection’s name.

Method Description

Get a Set of all documents in a collection.

Get a collection document by its document id.

Create a collection document from an object that may contain metadata fields.

Get the first collection document that matches a provided predicate.

Get a Set of collection documents that match a provided predicate.

Call an index as a method to get a Set of matching collection documents.

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!