set.forEach()
Iterate over all values in a Set, executing a function on each value.
Description
The forEach()
method iterates over all values in the Set and executes
the provided functionBody on each value. It is used for mutations or
writing documents based on each Set value.
Because this method scans the full set, it returns an error if there are more than 16,000 documents in the set. This method can timeout for large sets under that limit.
Iterator methods
FQL provides several methods for iterating over a set.
set.forEach()
and
set.map()
are similar but used for
different purposes:
Method | Primary use | Notes |
---|---|---|
Perform in-place writes on set elements. |
Doesn’t return a value. |
|
Return a new set with projected or transformed elements. |
Can’t perform writes. |
|
Similar to |
Can’t perform writes. |
Examples
Update all Customers documents where the first name is Carol
and the
last name is Clark
, to change the phone number to 1-555-555-1212
:
// Get a set of `Customer` collection documents with an
// `address` in the `state` of `DC`.
let customers = Customer.where(.address?.state == "DC")
// Use `forEach()` to update each document in the previous set.
customers.forEach(doc => doc.update({
address: {
street: doc?.address?.street,
city: doc?.address?.city,
state: "District of Columbia",
postalCode: doc?.address?.postalCode,
country: doc?.address?.country,
}
})) // `forEach()` returns `null`.
forEach
doesn’t return a value.
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!