Query data with FQL

Reference: FQL language reference, FQL API reference

Fauna Query Language (FQL) is a TypeScript-like language used to read and write data in Fauna:

// Calls the `all()` method on the `Product` collection.
// Returns all `Product` collection documents.
Product.all()

For a tutorial covering basic FQL queries, including document CRUD operations, see Perform basic operations.

For common FQL query patterns, see FQL query patterns.

Method chaining

You typically compose an FQL query by chaining methods to one or more collections. Expressions are evaluated from left to right.

// `firstWhere()` gets the first `Product` collection document with
// a `name` of `cups`. `update()` updates the document's `name`
// field value to `clear cups`. The `?.` operator only calls
// `update()` if `firstWhere()` returns a non-null value.
Product.firstWhere(.name == "cups")?.update({ name: "clear cups" })
See Field accessors and method chaining

Static typing

FQL is statically typed. Every expression has a data type that’s checked before evaluation. If Fauna detects a type mismatch, it rejects the query with an error.

Type checking helps catch errors early and consistently, saving you time during development.

See Static typing

Indexes

Indexes let you search and sort a collection’s documents in a performant way. Unindexed queries should be avoided.

See Indexes

Relationships

You create relationships by including a reference to a document in another document. This lets you model complex data structures without duplicating data across collections.

// Get a `Store` collection document.
let store = Store.byName("DC Fruits").first()

// Create a `Product` collection document that references
// the `Store` collection document in the `store` field.
Product.create({
 name: "limes",
 description: "Organic, 1 ct",
 store: store
})
See Document relationships

Projection

Projection lets you return only the specific fields you want from queries.

You can project results to dynamically resolve document relationships. This lets you traverse multiple deeply nested relationships in a single query.

// Uses projection to only return each document's `name`,
// `description`, and `store` fields. The `store` field
// contains a resolved `Store` collection document.
Product.byName("limes") { name, description, store }
See Projection and field aliasing

Pagination

Fauna provides pagination for queries that return large datasets. Fauna client drivers include methods for iterating through paginated query results.

See Pagination

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!