【问题标题】:How to get nested documents in FaunaDB with a filter?如何使用过滤器在 FaunaDB 中获取嵌套文档?
【发布时间】:2022-07-16 05:08:30
【问题描述】:

以下查询:

  Paginate(Documents(Collection("backyard"))),
  Lambda(
    "f",
    Let(
      {
        backyard: Get(Var("f")),
        user: Get(Select(["data", "user"], Var("backyard")))
      },
      {
        backyard: Var("backyard"),
        user: Var("user")
      }
    )
  )
) 

结果:

{
  data: [
    {
      backyard: {
        ref: Ref(Collection("backyard"), "333719283470172352"),
        ts: 1654518359560000,
        data: {
          user: Ref(Collection("user"), "333718599460978887"),
          product: "15358",
          date: "2022-06-06",
          counter: "1"
        }
      },
      user: {
        ref: Ref(Collection("user"), "333718599460978887"),
        ts: 1654517707220000,
        data: {
          email: "<email>",
          name: "Paolo"
        }
      }
    },
    {
      backyard: {
        ref: Ref(Collection("backyard"), "333747850716381384"),
        ts: 1654545603400000,
        data: {
          user: Ref(Collection("user"), "333718599460978887"),
          product: "15358",
          date: "2022-06-08",
          counter: "4"
        }
      },
      user: {
        ref: Ref(Collection("user"), "333718599460978887"),
        ts: 1654517707220000,
        data: {
          email: "<email>",
          name: "Paolo"
        }
      }
    }
  ]
}

如何在不丢失嵌套用户的情况下按日期过滤后院?

我试过了:

Map(
  Paginate(Range(Match(Index("backyard_by_date")), "2022-05-08", "2022-06-08")),
  Lambda(
    "f",
    Let(
      {
        backyard: Get(Var("f")),
        user: Get(Select(["data", "user"], Var("backyard")))
      },
      {
        backyard: Var("backyard"),
        user: Var("user")
      }
    )
  )
)

但是,结果集是一个空数组,以下已经返回一个空数组:

Paginate(Range(Match(Index("backyard_by_date")), "2022-05-08", "2022-06-08"))

我的索引:

{
  name: "backyard_by_date",
  unique: false,
  serialized: true,
  source: "backyard"
}

也许我必须调整我的索引?以下对我帮助很大:

【问题讨论】:

  • 您能否更新您的问题以包含“backyard_by_date”索引的定义?
  • @eskwayrd:我更新了问题并添加了索引定义。
  • 该索引定义没有指定terms。如果这就是你实际使用的,那就是问题所在。没有termsvalues 的索引称为“集合索引”:包含集合的所有文档,没有匹配的terms 对组进行子集化,默认结果包括文档引用,不能使用用于日期比较。也许在动物群论坛中提出您的问题,这更有利于最终达成解决方案的对话。

标签: faunadb


【解决方案1】:

您的索引定义缺少详细信息。一旦这个问题得到解决,你所做的一切都是正确的。

在您提供的索引中,没有指定termsvalues,这使得backyard_by_date 索引成为“集合”索引:它只记录集合中每个文档的引用。这样,它在功能上等同于使用 Documents 函数,但在 backyard 集合中创建或更新文档时会产生额外的写入操作。

要使您的查询正常工作,您应该删除现有索引并(60 秒后)重新定义它,如下所示:

CreateIndex({
  name: "backyard_by_date",
  source: Collection("backyard"),
  values: [
    {field: ["data", "date"]},
    {field: ["ref"]}
  ]
})

该定义将索引配置为返回 date 字段和每个文档的引用。

让我们确认索引返回了我们期望的结果:

> Paginate(Match(Index("backyard_by_date")))
{
  data: [
    [ '2022-06-06', Ref(Collection("backyard"), "333719283470172352") ],
    [ '2022-06-08', Ref(Collection("backyard"), "333747850716381384") ]
  ]
}

date字段的值放在首位意味着我们可以在Range中有效地使用它:

> Paginate(Range(Match(Index("backyard_by_date")), "2022-05-08", "2022-06-08"))
{
  data: [
    [ '2022-06-06', Ref(Collection("backyard"), "333719283470172352") ],
    [ '2022-06-08', Ref(Collection("backyard"), "333747850716381384") ]
  ]
}

并验证Range 是否按预期工作:

> Paginate(Range(Match(Index("backyard_by_date")), "2022-06-07", "2022-06-08"))
{
  data: [
    [ '2022-06-08', Ref(Collection("backyard"), "333747850716381384") ]
  ]
}

现在我们知道索引工作正常,您的过滤器查询需要进行一些调整:

> Map(
  Paginate(
    Range(Match(Index("backyard_by_date")), "2022-05-08", "2022-06-08")
  ),
  Lambda(
    ["date", "ref"],
    Let(
      {
        backyard: Get(Var("ref")),
        user: Get(Select(["data", "user"], Var("backyard")))
      },
      {
        backyard: Var("backyard"),
        user: Var("user")
      }
    )
  )
)
{
  data: [
    {
      backyard: {
        ref: Ref(Collection("backyard"), "333719283470172352"),
        ts: 1657918078190000,
        data: {
          user: Ref(Collection("user"), "333718599460978887"),
          product: '15358',
          date: '2022-06-06',
          counter: '1'
        }
      },
      user: {
        ref: Ref(Collection("user"), "333718599460978887"),
        ts: 1657918123870000,
        data: { name: 'Paolo', email: '<email>' }
      }
    },
    {
      backyard: {
        ref: Ref(Collection("backyard"), "333747850716381384"),
        ts: 1657918172850000,
        data: {
          user: Ref(Collection("user"), "333718599460978887"),
          product: '15358',
          date: '2022-06-08',
          counter: '4'
        }
      },
      user: {
        ref: Ref(Collection("user"), "333718599460978887"),
        ts: 1657918123870000,
        data: { name: 'Paolo', email: '<email>' }
      }
    }
  ]
}

由于索引返回日期字符串和引用,Map 内的Lambda 必须接受这些值作为参数。除了将 f 重命名为 ref 之外,您的查询的其余部分保持不变。

【讨论】:

    猜你喜欢
    • 2020-07-21
    • 2017-07-04
    • 2021-04-07
    • 2021-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-02
    相关资源
    最近更新 更多