首页 > 解决方案 > 如何在 .where 之后访问 firestore 中的子集合

问题描述

我有一个集合和一个名称数组,集合可以通过 id 直接查询

我想根据名称访问唯一的子集合,并且我想在之后返回子集合

查询的结构是这样的

firestore
.collection
.where("name", '==', inputName)
.collection('subCollection')
.get()

这似乎不起作用,是否有替代方法

标签: javascriptdatabasegoogle-cloud-platformgoogle-cloud-firestore

解决方案


Firestore 查询只能检查它们返回的文档中的数据。无法从子集合中返回文档,也无法从父文档中筛选字段。

常见的解决方案是将 也添加name到子集合中的每个文档(例如作为名为 的字段parentName),然后对名为 的所有集合执行集合组查询subCollection

firestore
.collectionGroup('subCollection')
.where("parentName", '==', inputName)
.get()

推荐阅读