collection.where()

Learn: Documents

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.

A document’s data type is taken from its collection’s name. For example, Product for a document in the Product collection. See Document type.

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")
    }
  ]
}
\