DEV Community

Cover image for How to do simple updates in Fauna Query Language (FQL)
Kirk Kirkconnell
Kirk Kirkconnell

Posted on • Updated on

How to do simple updates in Fauna Query Language (FQL)

Now that we have written some data to the database, let’s look at updating existing data.

In the previous example, we created a generic array to use, but in this example, we will use the demo data you can load when creating a new database in Fauna’s dashboard. Let’s update some of the order documents in the Order collection. More specifically, we want to update the status field to “shipped” for every order in our system currently with a status of “processing” and has a delivery address with the zip code “20220.” In this example, I am querying an index I created on the Order collection to get a set of documents.

Order.byStatus("processing", "20220").toArray().map(doc => doc.update({status: "shipped"}))
Enter fullscreen mode Exit fullscreen mode

The byStatus index is defined with two index terms on the fields status and the zipCode in the deliveryAddress object nested in the Order documents. I use FQL’s .toArray() function to materialize the Set I get from reading the index into an array of documents. If I call the .map() function without the .toArray() function, the call will fail as this could, in rare cases, help you create undesirable data consistency issues, so FQL prohibits this action. Anyhow, I want to update the array of documents at the exact same time, and I know there are not that many of them. Therefore, toArray() materializes the result set into one big array, and map iterates through that array to run the update function on each document altering the status to shipped.

One more nuance worth noting, I am using the .map() function instead of the .forEach() function you saw earlier for a specific reason. forEach can be used to update the documents too, but it doesn’t return the updated documents written to the database, whereas .map() does. This is an important distinction between these two functions, so use the right one in your app depending on what you need. Just to be clear, these two functions are for more than just write operations in FQL. They are used to control flow and to iterate through a result from any operation on a SET and then run other FQL functions or just return the results. For example, here is a read operation from that same index, but I am using .map() and projecting just the customer field of the documents returned from the index.

Order.byStatus("shipped", "20220").map(doc => "#{doc.customer}")
Enter fullscreen mode Exit fullscreen mode

The reason I am pointing this out is to reinforce that functions in FQL can have many uses, and you’ll use them often within Fauna.


Fauna is a serverless, globally distributed strong consistent database designed for modern application development. It offers the flexibility of NoSQL with the safety and ease of traditional relational databases. It provides seamless scalability, strong consistency, and multi-model access capabilities, making it an ideal choice for developers seeking to build fast, reliable applications without the operational overhead of traditional database management. Fauna's support for ACID transactions, and its temporal database features further enhance its utility, allowing for efficient data retrieval, updates, and historical data analysis.

Top comments (0)