[英]How to get specific Documents of a Subcollection of all Documents in a Collection in Firestore?
因此,我以这种方式订购了我的数据:
MyCollection //main collection
--(All documents) //I want all documents of main collection
--publicSubcollection //I want ONLY publicSubcollection of those documents
--(All documents) //I want all documents of publicSubcollection
这是我在我的应用程序中的操作方式:
query = firestore().collection('MyCollection').doc().collection('publicSubcollection')
.where('act', '==', 1)
.where('city', '==', this.state.selected_city)
.orderBy('update_time', 'desc')
.limit(10);
query.onSnapshot({
//do something
})
它什么也没返回,空的 object。 我尝试使用 Postman 调用 REST Api 并且它还返回空的 ZA8CFDE6331BD54B66AC96F8911C。 我尝试只调用“MyCollection”,它会返回一些我在那里的数据。 那么,我如何才能真正查询特定子集合中的文档呢? 我在这段代码中做错了什么?
在 Firestore 查询中使用没有参数的doc()
毫无意义。 如果没有参数,它会生成一个带有随机文档 ID 的 DocumentReference。 该 ID 肯定不存在,您的查询也不会返回任何结果。 如果要查询子集合,则需要能够构建到该子集合的路径,包括该路径中的所有文档 ID。 Firestore 路径中没有通配符匹配项。 换句话说,您需要将一个字符串传递给doc()
以标识要查询的文档的子集合。 该文档不需要存在 - 它只需要命名。
如果您实际上是在尝试查询名为“publicSubcollection”的所有子集合中的所有文档,则需要使用集合组查询。 它可能看起来像这样:
query = firestore()
.collectionGroup('publicSubcollection')
.where('act', '==', 1)
.where('city', '==', this.state.selected_city)
.orderBy('update_time', 'desc')
.limit(10);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.