Reputation: 609
I have a collection of meals which contain a date field 'date'. I want to query all meals within a specific date range. I struggle to build a FQL query that returns the whole documents for the use in a user generated function to be consumed in a GraphQL query.
What i have:
Index 'mealsByDate':
{
name: "mealsByDate",
unique: false,
serialized: true,
source: "Meal",
terms: [],
values: [
{
field: ["data", "date"]
},
{
field: ["ref"]
}
]
}
Query:
Paginate(Range(Match(Index("mealsByDate")), Date("2019-12-30"), Date("2020-01-05")))
Result:
{
"data": [
[
Date("2019-12-30"),
Ref(Collection("Meal"), "253389516394463764")
],
[
Date("2019-12-30"),
Ref(Collection("Meal"), "253389626235945490")
],
[
Date("2020-01-05"),
Ref(Collection("Meal"), "253389653063762452")
]
]
}
How can i get the documents of the refs in the result set? I tried to put the Paginate function into a Map function and apply a Lambda which does a Get, but i'm not able to select the ref in the result. Or is the whole approach flawed?
Upvotes: 2
Views: 890
Reputation: 4521
When you paginate results from an index that contains multiple value
fields, each result is an array of the specified values from the indexed document. As your query demonstrated, your index returns arrays with 2 values that contain the date and document ref.
When you wrap your query with Map
, the lambda function that you provide must accept the same number of arguments, like so:
Map(
Paginate(
Range(
Match(Index("mealsByDate")),
Date("2019-12-30"),
Date("2020-01-05")
)
),
Lambda(
["d", "r"],
Get(Var("r"))
)
)
With that query, the date field as passed as the argument d
, and the ref is passed as the argument r
. That makes it easy to access the ref, using the Var
function.
When you use Map
to iterate over other structures that might include an array, you can use the Select
function to access numbered elements of the array. See https://docs.fauna.com/fauna/current/api/fql/functions/select for more details.
Upvotes: 2