我有一个名为“user-followers”的集合。我基本上需要显示关注者总数。只是无法找出完成 block 来获得计数。有一些资源可以帮助我获取集合下文档的数量,但文档中的值又如何呢?这是数据在 firestore 中的存储方式:
Collection: "user-followers"
Document: "abc"
"user1":1
"user2":1
"user3":1
我想要文档“abc”下的用户数,即 3。
有一些资源可以计算所有文档的数量,但文档中数据的数量又如何呢。
最佳答案
我不确定这样存储您的数据是最好的主意,但您似乎在询问如何计算文档中的字段数。
一个简单的解决方案是计算子数据,在本例中为三个。
func countFields() {
let collectionRef = self.db.collection("user-followers")
let documentRef = collectionRef.document("doc")
documentRef.getDocument(completion: { documentSnapshot, error in
if let error = error {
print(error.localizedDescription)
return
}
let q = documentSnapshot?.data()?.count
print(q)
})
}
documentSnapshot?.data() 是一个可以迭代、计数等的字典
关于ios - 如何获取集合下文档下的数据计数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56953210/