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.
-
Create the document using the following coffee grower data:
-
This time, when you call the
all()method, you should see the document created in the previous tutorial and the newly created document:{ 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 } } ] } -
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 of1to get the first document in the set:{ 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 } } ] } -
As another example, use the
where()method to select all documents that have matching data field values:{ 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_Originvalue matches the query parameter. If the database has multiple documents with the sameCountry_of_Originvalue, 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:
{
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:
{
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!