Iterate on sets

Here are some useful operations for working with Sets of documents. With Set methods, you can conveniently find and change documents using selection criteria.

Setup

You already have a set of Documents in the CoffeeBean collection from the Fauna at-a-glance tutorial. If not, return to that tutorial and create two CoffeeBean documents before continuing.

  1. When you’re ready, call CoffeeBean.all() to verify that you have at least two documents then add another document with the following data for data variance:

    CoffeeBean.create({
      {
        "Species": "Robusta",
        "Owner": "nishant gurjer",
        "Country_of_Origin": "India",
        "Harvest_Year": 2017,
        "Quality_Parameters": {
          "Aroma": 8,
          "Flavor": 7.75,
          "Balance": 7.92
        },
        "Altitude": {
          "unit_of_measurement": "m",
          "mean": 3170
        }
      }
    })
  2. Call CoffeeBean.all() again to see your new document added to the set.

Get documents that match selection criteria

In the previous tutorial, you used the take() method to get the number of documents you wanted from the set, starting with the first document. You also used the byId() method because you knew the document ID. Here, call the where() method to get documents that match your selection criteria based on field values. Select on coffee species:

CoffeeBean.all().where(.Species == "Robusta")
{
  data: [
    {
      id: "366521185649819716",
      coll: CoffeeBean,
      ts: Time("2023-06-03T13:58:11.180Z"),
      Species: "Robusta",
      Owner: "nishant gurjer",
      Country_of_Origin: "India",
      Harvest_Year: 2017,
      Quality_Parameters: {
        Aroma: 8,
        Flavor: 7.75,
        Balance: 7.92
      },
      Altitude: {
        unit_of_measurement: "m",
        mean: 3170
      }
    }
  ]
}

This should return only the Robusta species document, which is the document you previously added.

The CoffeeBean.all() call on the collection returns a Set, where you can apply the Set methods, including where().

The where() method searches all documents in your collection, which can significantly impact performance and cost. Usually, you want to use Indexes, instead, which are covered in the next tutorial.

Update select documents

You can also use Set methods to change data in your selected documents.

This exercise again uses the where() method to select your documents and the forEach() method to apply an anonymous function to update the data.

  1. For every document with a Flavor quality greater than eight, add a Best_of_Class field that has the Date today:

    CoffeeBean.all()
      .where(.Quality_Parameters.Flavor > 8.0)
      .forEach(bean => {
        bean.update({ Best_of_Class: Date.today() })
    })
  2. Notice that only the selected documents have a new Best_of_Class field:

    CoffeeBean.all()
    {
      data: [
        {
          id: "366190504817197124",
          coll: CoffeeBean,
          ts: Time("2023-06-03T14:36:57.370Z"),
          Owner: "metad plc",
          Country_of_Origin: "Ethiopia",
          Species: "Arabica",
          Harvest_Year: 2014,
          Quality_Parameters: {
            Aroma: 8.67,
            Flavor: 8.83,
            Balance: 8.42
          },
          Altitude: {
            unit_of_measurement: "m",
            mean: 2075
          },
          Best_of_Class: Date("2023-06-03")
        },
        {
          id: "366190711733747780",
          coll: CoffeeBean,
          ts: Time("2023-06-03T14:36:57.370Z"),
          Species: "Arabica",
          Owner: "Healthy Grounds",
          Country_of_Origin: "Guatemala",
          Harvest_Year: "",
          Quality_Parameters: {
            Aroma: 8.42,
            Flavor: 8.5,
            Balance: 8.42
          },
          Altitude: {
            unit_of_measurement: "m",
            mean: 1700
          },
          Best_of_Class: Date("2023-06-03")
        },
        {
          id: "366521185649819716",
          coll: CoffeeBean,
          ts: Time("2023-06-03T14:34:18.240Z"),
          Species: "Robusta",
          Owner: "nishant gurjer",
          Country_of_Origin: "India",
          Harvest_Year: 2017,
          Quality_Parameters: {
            Aroma: 8,
            Flavor: 7.75,
            Balance: 7.92
          },
          Altitude: {
            unit_of_measurement: "m",
            mean: 3170
          },
        }
      ]
    }

Create a set from an existing set

While you can use the forEach() method to update data in an existing set, as you did in the previous exercise, you might want to use the map() method to create a new set from your original set. The forEach() method also applies an anonymous function to the original set.

Create a set that describes the best-of-class coffee growers:

CoffeeBean.all()
  .where(.Best_of_Class != null)
  .map(grower => grower.Owner + " grows " + grower.Species)
{
  data: [
    "metad plc grows Arabica",
    "Healthy Grounds grows Arabica"
  ]
}

Next steps

While the Set methods offer convenient and powerful operations for working with groups of documents, you might be able to do similar tasks more efficiently using indexes. The Improve performance with indexes tutorial can help you understand and evaluate the two approaches.

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!