在日期范围内加载 FaunaDB 文档。

huangapple go评论124阅读模式
英文:

Load FaunaDB documents within date range

问题

我有一个包含日期字段'date'的餐饮集合。我想查询特定日期范围内的所有餐饮。我难以构建一个返回完整文档的FQL查询,以供在GraphQL查询中使用的用户生成的函数消耗。

我拥有的内容:

索引'mealsByDate':

{
  "name": "mealsByDate",
  "unique": false,
  "serialized": true,
  "source": "Meal",
  "terms": [],
  "values": [
    {
      "field": ["data", "date"]
    },
    {
      "field": ["ref"]
    }
  ]
}

查询:

Paginate(Range(Match(Index("mealsByDate")), Date("2019-12-30"), Date("2020-01-05")))

结果:

{
  "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")
    ]
  ]
}

如何获取结果集中引用的文档?我尝试将Paginate函数放入Map函数中,并应用一个执行Get操作的Lambda,但我无法选择结果中的引用。或者整个方法是否有缺陷?

英文:

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?

答案1

得分: 2

当您分页检索包含多个“value”字段的索引的结果时,每个结果都是来自索引文档的指定值的数组。正如您的查询所示,您的索引返回包含日期和文档引用的2个值的数组。

当您使用Map包装您的查询时,您提供的lambda函数必须接受相同数量的参数,如下所示:

Map(
  Paginate(
    Range(
      Match(Index("mealsByDate")),
      Date("2019-12-30"),
      Date("2020-01-05")
    )
  ),
  Lambda(
    ["d", "r"],
    Get(Var("r"))
  )
)

使用该查询时,日期字段作为参数d传递,引用作为参数r传递。这使得通过Var函数轻松访问引用。

当您使用Map来迭代可能包含数组的其他结构时,您可以使用Select函数来访问数组的编号元素。有关更多详细信息,请参阅https://docs.fauna.com/fauna/current/api/fql/functions/select。

英文:

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.

huangapple
  • 本文由 发表于 2020年1月3日 16:41:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/59575436.html
  • faunadb
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定