![](/img/trans.png)
[英]How to get root document from sub collection documento in firestore?
[英]How to filter root collection on the basis of values in the sub collection in Firestore?
我在 Swift iOS 中使用 Cloud Firestore 数据库,我想根据每个文档的子集合中可用的值过滤根集合列表?
根集合--> 帖子--> 文档--> 子集合--> 文档--> 字段值
“fieldvalue”的值必须决定根集合帖子中的文档列表
我在下面尝试了这个,但它没有按要求工作。
let postsRef = Firestore.firestore()
.collection("posts")
.document()
.collection("comments")
.whereField("comment_author_id", isEqualTo: Auth.auth().currentUser!.uid)
.whereField("l1", isEqualTo: true)
.limit(to: 50)
请协助
您的查询未按要求工作,因为调用时:
.document()
紧接着:
.collection("posts")
在不传递任何参数的情况下,Firestore 并不真正知道您要引用哪个文档。 如果您想获得2czkDrrg4gHgXbCID69u
文档中的所有评论,那么您应该将该 id 传递给document()
function,如下所示:
.collection("posts").document("2czkDrrg4gHgXbCID69u")
请记住,Firestore 查询很浅,只能从运行查询的集合中获取评论。 单个查询只能使用单个集合中文档的属性。
如果您想在您的posts
集合中存在的所有文档中获取所有评论,那么您应该使用Firestore 集合组查询。 请注意,所有子集合必须具有相同的(评论)名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.