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.where()

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

Signature

where(pred: (<Document> => Boolean)) => Set<Document>

Description

Gets a Set of collection documents that match a provided predicate function.

If this method is the last value in a query, the first page of the Set is returned. See Pagination.

Performance hint: collection_scan

Queries that call this method emit a performance hint, if enabled. For example:

performance_hint: collection_scan - Using where() on collection Product can cause a read of every document. See https://docs.fauna.com/performance_hint/collection_scan.
at *query*:1:14
  |
1 | Product.where(.name == "limes")
  |              ^^^^^^^^^^^^^^^^^^
  |

To address the hint, create an index definition with a terms to look up matching documents instead. For example:

collection Product {
  ...
  // Adds `name` as an index term
  index byName {
    terms [.name]
    values [.name, .price, .description, .stock]
  }
  ...
}

Then call the index in your query. Pass an argument for each term in the index definition. To avoid other performance hints, only project or map field values covered by the index definition’s values:

// Get products named "limes"
Product.byName("limes") {
  name,
  price,
  description,
  stock
}

Parameters

Parameter Type Required Description

pred

Predicate function

Yes

Anonymous predicate function that:

* Accepts a collection document as its only argument. Supports shorthand-syntax. * Returns a Boolean value.

The method returns collection documents for which the predicate returns true.

Return value

Type Description

Set<Document>

Set of collection documents that match the predicate. If there are no matching documents, the Set is empty.

Examples

Product.where(.stock < 20)
{
  data: [
    {
      id: "999",
      coll: Product,
      ts: Time("2099-07-30T21:56:38.130Z"),
      name: "taco pinata",
      description: "Giant Taco Pinata",
      price: 2399,
      stock: 10,
      category: Category("123")
    }
  ]
}

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!