[英]How to get specific Documents of a Subcollection of all Documents in a Collection in Firestore?
So, I have my data ordered in this way:因此,我以这种方式订购了我的数据:
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
And here is how I am doing it in my app:这是我在我的应用程序中的操作方式:
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
})
and it is returning nothing, empty object.它什么也没返回,空的 object。 I tried calling REST Api using Postman and it also returns empty object.
我尝试使用 Postman 调用 REST Api 并且它还返回空的 ZA8CFDE6331BD54B66AC96F8911C。 I tried by calling only 'MyCollection' and it returns some data I have in there.
我尝试只调用“MyCollection”,它会返回一些我在那里的数据。 So, how can I actually query documents in a specific sub-collection?
那么,我如何才能真正查询特定子集合中的文档呢? What am I doing wrong in this code?
我在这段代码中做错了什么?
It never makes sense to use doc()
without an argument in a Firestore query.在 Firestore 查询中使用没有参数的
doc()
毫无意义。 Without an argument, it generates a DocumentReference with a random document ID.如果没有参数,它会生成一个带有随机文档 ID 的 DocumentReference。 That ID will certainly not exist, and your query will not return any results.
该 ID 肯定不存在,您的查询也不会返回任何结果。 If you want to query a subcollection, you need to be able to build a path to that subcollection, including all document IDs in that path.
如果要查询子集合,则需要能够构建到该子集合的路径,包括该路径中的所有文档 ID。 There are no wildcard matches in Firestore paths.
Firestore 路径中没有通配符匹配项。 In other words, you need to pass a string to
doc()
that identifies which document's subcollection to query.换句话说,您需要将一个字符串传递给
doc()
以标识要查询的文档的子集合。 That document doesn't need to exist - it just needs to be named.该文档不需要存在 - 它只需要命名。
If you are actually trying to query all documents in all subcollections called "publicSubcollection", you'll need to use a collection group query instead.如果您实际上是在尝试查询名为“publicSubcollection”的所有子集合中的所有文档,则需要使用集合组查询。 It might looks like this:
它可能看起来像这样:
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.