collection.firstWhere()

Get the first collection document that matches a provided predicate.

Signature

firstWhere(pred: (<Document> => Boolean)) => <Document> | <NullDoc>

Description

Gets the first collection document that matches a provided predicate function.

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 the first collection document for which the predicate returns true.

Return value

One of:

Type Description

Document

The first collection document that matches the predicate.

NullDoc

Document doesn’t exist. Returned when no collection document matches the predicate. See NullDoc.

Examples

Product.firstWhere(.stock < 20)
{
  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!