【问题标题】:Firestore fetch data from inner array of the docFirestore 从文档的内部数组中获取数据
【发布时间】:2018-07-30 15:11:30
【问题描述】:

我知道如何像这样查询简单的集合/文档:

const querySnapshot = await 
this.authenticationProvider.firestoreDb.collection("members").where("userId", 
"==", this.authenticationProvider.user.uid).get();

但是我可以使用firestore 上的条件从内部数组中获取数据吗? 您可以看到我需要检查projects/memberList 数组的位置,然后需要搜索email。我可以这样做吗?

【问题讨论】:

  • 放 gif 的好地方 :) 。您可以使用点. 深入了解阵列,检查此link。但是你有一个递增的动态列表。即使您动态查询数据,也无法index 它并且firestore 抛出错误。
  • 实际上,我需要检索整个文档。但是我需要在projects/memberListemail 之后的2 个数组之后给出搜索条件。不过,我需要检索集合。我可否? @SurajRao
  • 这里的问题是,由于有 2 个数组,我如何在那里给出搜索条件? @SurajRao
  • 我认为你做不到。您可以获取文档,然后使用 TypeScript 过滤掉不需要的文档。

标签: angular typescript firebase ionic3 google-cloud-firestore


【解决方案1】:

你不能用数组来做到这一点。您需要将数据构造为一个对象,其中每个键都是userId。 Firestore 将索引对象键,允许您对它们运行查询。您的数据结构可能如下所示:

然后你可以运行这样的查询:

const userId = this.authenticationProvider.user.uid
ref.collection('members').where(`memberList.${userId}`, '==', true)

您甚至可以在每个 userId 键下嵌入额外的数据(例如另一个对象),然后像这样获取该用户所属的所有文档:

ref.orderBy(`memberList.${userId}`)

【讨论】:

猜你喜欢
  • 2020-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-13
  • 2020-09-07
  • 2023-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多