1

我有两个集合,其中包含以下格式的数据

{
  "ref": Ref(Collection("Leads"), "267824207030650373"),
  "ts": 1591675917565000,
  "data": {
    "notes": "voicemail ",
    "source": "key-name",
    "name": "Glenn"
  }
}

{
  "ref": Ref(Collection("Sources"), "266777079541924357"),
  "ts": 1590677298970000,
  "data": {
    "key": "key-name",
    "value": "Google Ads"
  }
}

我希望能够查询Leads集合并能够Sources在单个查询中检索相应的文档

我想出了以下查询来尝试使用索引,但我无法让它运行

Let(
      {
        data: Get(Ref(Collection('Leads'), '267824207030650373'))
      },
      {
        data: Select(['data'],Var('data')),
        source: q.Lambda('data',
              Match(Index('LeadSourceByKey'), Get(Select(['source'], Var('data') )) )
            )
      }
)

有没有一种简单的方法来检索 Sources 文档?

4

2 回答 2

2

您正在寻找的是我在多个步骤中为您分解的以下查询:

Let(
  {
    // Get the Lead document 
    lead: Get(Ref(Collection("Leads"), "269038063157510661")),
    // Get the source key out of the lead document
    sourceKey: Select(["data", "source"], Var("lead")),
    // use the index to get the values via match 
    sourceValues: Paginate(Match(Index("LeadSourceValuesByKey"), Var("sourceKey")))
  },
  {
    lead: Var("lead"),
    sourceValues: Var("sourceValues")
  }
)

结果是:

{
  lead: {
    ref: Ref(Collection("Leads"), "269038063157510661"),
    ts: 1592833540970000,
    data: {
      notes: "voicemail ",
      source: "key-name",
      name: "Glenn"
    }
  },
  sourceValues: {
    data: [["key-name", "Google Ads"]]
  }
}

sourceValues 是一个数组,因为您在索引中指定将返回两个项目,即键和值,并且索引始终返回该数组。由于您的Match在不是一对一的情况下可能会返回多个值,因此这将成为数组的数组。

这只是一种方法,您还可以让索引返回一个参考和Map/Get以获取实际文档,如论坛中所述。

但是,我假设您在这里问了同样的问题。尽管我很高兴在 stackoverflow 与 slack 甚至我们自己的论坛上提出问题,但请不要在没有链接到其他人的情况下到处发布相同的问题。这使得许多人花费大量时间,而问题已经在其他地方得到了回答。

于 2020-06-22T16:20:39.923 回答
0

您可能会更改 Leads 文档并将 Ref to Sources 文档放在源代码中:

{
  "ref": Ref(Collection("Leads"), "267824207030650373"),
  "ts": 1591675917565000,
  "data": {
    "notes": "voicemail ",
    "source": Ref(Collection("Sources"), "266777079541924357"),
    "name": "Glenn"
  }
}

{
  "ref": Ref(Collection("Sources"), "266777079541924357"),
  "ts": 1590677298970000,
  "data": {
    "key": "key-name",
    "value": "Google Ads"
  }
}

然后这样查询:

Let(
      {
        lead: Select(['data'],Get(Ref(Collection('Leads'), '267824207030650373'))),
        source:Select(['source'],Var('lead'))
      },
      {
        data: Var('lead'),
        source: Select(['data'],Get(Var('source')))
      }
)
于 2020-06-22T13:23:11.220 回答