gpt4 book ai didi

ios - 如何根据 Firestore 子集合中的值过滤根集合?

转载 作者:搜寻专家 更新时间:2023-11-01 06:49:56 26 4
gpt4 key购买 nike

我在 Swift iOS 中使用 Cloud Firestore 数据库,我想根据每个文档子集合中可用的值过滤根集合列表?

根集合--> 帖子--> 文档--> 子集合--> 文档--> 字段值

“字段值”的值必须决定根集合帖子中的文档列表

我在下面尝试了这个,但它没有按要求工作。

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)

请帮忙

enter image description here

enter image description here

最佳答案

您的查询未按要求工作,因为调用时:

.document()

紧接着:

.collection("posts")

在不传递任何参数的情况下,Firestore 并不知道您正在引用哪个文档。如果您想在您的 2czkDrrg4gHgXbCID69u 文档中获取所有评论,那么您应该将该 ID 传递给 document() 函数,如下所示:

.collection("posts").document("2czkDrrg4gHgXbCID69u")

请记住,Firestore 查询很浅,只能从运行查询的集合中获取评论。单个查询只能使用单个集合中文档的属性。

如果您想要获取所有文档中存在于您的posts 集合中的所有评论,那么您应该使用Firestore collection group query。 .请注意,所有子集合必须具有相同的(注释)名称。

关于ios - 如何根据 Firestore 子集合中的值过滤根集合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58215112/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com