Query on collections

This tutorial shows you how to select one or more documents using the FQL Set methods.

The Use indexes for more efficient queries tutorial shows you how to improve on using FQL set methods by using indexes.

Select documents in a collection

Start by adding a second document to your CoffeeBean collection, viewing the documents in the collection, and selecting one of the documents.

  1. Create the document using the following coffee grower data:

    CoffeeBean.create({
        "Species": "Arabica",
        "Owner": "grounds for health admin",
        "Country_of_Origin": "Guatemala",
        "Harvest_Year": "",
        "Quality_Parameters": {
          "Aroma": 8.42,
          "Flavor": 8.5,
          "Balance": 8.42
        },
        "Altitude": {
          "unit_of_measurement": "m",
          "mean": 1700
        }
    })
  2. This time, when you call the all() method, you should see the document created in the previous tutorial and the newly created document:

    CoffeeBean.all()
    {
      data: [
        {
          id: "378484096953745473",
          coll: CoffeeBean,
          ts: Time("2023-10-13T15:03:32.470Z"),
          Species: "Arabica",
          Owner: "metad plc",
          Country_of_Origin: "Ethiopia",
          Harvest_Year: 2014,
          Quality_Parameters: {
            Aroma: 8.67,
            Flavor: 8.83,
            Balance: 8.42
          },
          Altitude: {
            unit_of_measurement: "m",
            mean: 2075
          }
        },
        {
          id: "378507396792713281",
          coll: CoffeeBean,
          ts: Time("2023-10-13T21:13:52.930Z"),
          Species: "Arabica",
          Owner: "grounds for health admin",
          Country_of_Origin: "Guatemala",
          Harvest_Year: "",
          Quality_Parameters: {
            Aroma: 8.42,
            Flavor: 8.5,
            Balance: 8.42
          },
          Altitude: {
            unit_of_measurement: "m",
            mean: 1700
          }
        }
      ]
    }
  3. Using built-in Set methods, you can select one or more documents from the set that you want to view or change. This example uses the take() method with a parameter of 1 to get the first document in the set:

    CoffeeBean.all().take(1)
    {
      data: [
        {
          id: "378484096953745473",
          coll: CoffeeBean,
          ts: Time("2023-10-13T15:03:32.470Z"),
          Species: "Arabica",
          Owner: "metad plc",
          Country_of_Origin: "Ethiopia",
          Harvest_Year: 2014,
          Quality_Parameters: {
            Aroma: 8.67,
            Flavor: 8.83,
            Balance: 8.42
          },
          Altitude: {
            unit_of_measurement: "m",
            mean: 2075
          }
        }
      ]
    }
  4. As another example, use the where() method to select all documents that have matching data field values:

    CoffeeBean.all().where(.Country_of_Origin == "Guatemala")
    {
      data: [
        {
          id: "378507396792713281",
          coll: CoffeeBean,
          ts: Time("2023-10-13T21:13:52.930Z"),
          Species: "Arabica",
          Owner: "grounds for health admin",
          Country_of_Origin: "Guatemala",
          Harvest_Year: "",
          Quality_Parameters: {
            Aroma: 8.42,
            Flavor: 8.5,
            Balance: 8.42
          },
          Altitude: {
            unit_of_measurement: "m",
            mean: 1700
          }
        }
      ]
    }

    Fauna returns the second document of the set whose Country_of_Origin value matches the query parameter. If the database has multiple documents with the same Country_of_Origin value, all matching documents are retrieved.

    To learn more about FQL dot notation and method chaining, see the Field accessors and method chaining reference documentation.

Get document data using projection

By default, a query on a document returns all document fields, but you can also get only those fields you want from the documents using projection. Projection allows you to get only those fields you want from the set of documents you selected.

Get the Species and Country_of_Origin fields from your two CoffeeBean documents:

CoffeeBean.all().take(2) {
  Species,
  Country_of_Origin
}
{
  data: [
    {
      Species: "Arabica",
      Country_of_Origin: "Ethiopia"
    },
    {
      Species: "Arabica",
      Country_of_Origin: "Guatemala"
    }
  ]
}

Create a document from an existing set

Other Set methods allow you to get creative in constructing new documents from existing data. In this example, the map() method is used to create a document that has the name of the coffee bean grower and the species they grow:

CoffeeBean.all()
  .map(grower => grower.Owner + " grows " + grower.Species)
{
  data: [
    "metad plc grows Arabica",
    "grounds for health admin grows Arabica"
  ]
}

The map() method takes an anonymous function parameter that selects the fields in the collection documents.

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!