FQL v4 will be decommissioned on June 30, 2025. Ensure that you complete your migration from FQL v4 to FQL v10 by that date.

Fauna accounts created after August 21, 2024 must use FQL v10. These accounts will not be able to run FQL v4 queries or access the v4 Dashboard.

For more details, see the v4 EOL announcement and migration guide. Contact support@fauna.com with any questions.

Filter

This reference topic applies to FQL v4. Go to this page for the latest FQL v10 reference topics.

Filter( arrayOrSet, lambda )
filter_( lambda, array_or_set )
Filter( arrayOrSet, lambda )
Filter( arrayOrSet, lambda )
Filter( arrayOrSet, lambda )
Filter( arrayOrSet, lambda )

Description

The Filter function applies the Lambda function to each member of arrayOrSet, which is an Array, Page, or Set. The return value matches the arrayOrSet type, and contains only those elements for which the lambda function returns true.

Providing a lambda function which does not return a Boolean results in an "invalid argument" error. If a Page is passed, its decorated fields are preserved in the result.

The run time of Filter is dependent on the size of the underlying set or page, and the exclusivity of the result. For large sets or pages with many non-exclusive items, executing Filter might result in a query timeout error.

For query timeout errors, you may specify a larger query timeout via the driver that you are using.

Parameters

Parameter Type Definition and Requirements

arrayOrSet

Array, Page, or Set

The group of items over which the lambda function iterates/operates.

lambda

The anonymous function to be executed, which must return a boolean.

Returns

Having the same type as arrayOrSet, the items for which the lambda function returned true.

Examples

The following query iterates over the array containing the values 1, 2, 3, executing the Lambda function for each value. The Lambda function returns true if the Modulo 2 of the value is 0, otherwise it returns false.

client.query(
  q.Filter(
    [1, 2, 3],
    q.Lambda(
      'i',
      q.Equals(0, q.Modulo(q.Var('i'), 2)),
    ),
  )
)
.then((ret) => console.log(ret))
.catch((err) => console.error(
  'Error: [%s] %s: %s',
  err.name,
  err.message,
  err.errors()[0].description,
))
[ 2 ]
result = client.query(
  q.filter_(
    q.lambda_(
      "i",
      q.equals(0, q.modulo(q.var("i"), 2))
    ),
    [1, 2, 3]
  )
)
print(result)
[ 2 ]
result, err := client.Query(
	f.Filter(
		f.Arr{1, 2, 3},
		f.Lambda(
			"i",
			f.Equals(0, f.Modulo(f.Var("i"), 2)),
		),
	))

if err != nil {
	fmt.Fprintln(os.Stderr, err)
} else {
	fmt.Println(result)
}
[2]
try
{
    Value result = await client.Query(
        Filter(
            Arr(1, 2, 3),
            Lambda(
                "i",
                EqualsFn(0, Modulo(Var("i"), 2))
            )
        )
    );
    Console.WriteLine(result);
}
catch (Exception e)
{
    Console.WriteLine($"ERROR: {e.Message}");
}
Arr(LongV(2))
System.out.println(
    client.query(
        Filter(
            Arr(Value(1), Value(2), Value(3)),
            Lambda(
                Value("i"),
                Equals(Value(0), Modulo(Var("i"), Value(2)))
            )
        )
    ).get());
[2]
Filter(
  [1, 2, 3],
  Lambda(
    "i",
    Equals(0, Modulo(Var("i"), 2)),
  ),
)
[ 2 ]
Query metrics:
  •    bytesIn:  95

  •   bytesOut:  16

  • computeOps:   1

  •    readOps:   0

  •   writeOps:   0

  •  readBytes:   0

  • writeBytes:   0

  •  queryTime: 7ms

  •    retries:   0

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!