我正在尝试过滤 Firestore 中的数据,当我将信息打印到控制台时,我得到了所有文档。 Firestore 数据库的结构如下:
/Collection
/auto-doc ID
/ hosp : "hosp1"
team : "team1"
(there are more fields in every document)
出于测试目的,我只有六个文档,其中两个具有我要过滤并打印到控制台 (hosp1) 的字段。
@IBAction func getData2(_ sender: Any) {
if HOSP != "hosp1" {
query = Firestore.firestore().collection(PTLIST_REF).whereField("hosp", isEqualTo: "hosp1")
ptListCollectionRef.getDocuments { (snapshot, error) in
if let err = error {
debugPrint("error getting data: \(err)")
}
else {
for document in (snapshot?.documents)! {
print(document.data())
}
}
最佳答案
您正在获取所有文档,因为您是在集合引用上而不是在查询上调用 getDocuments()
函数。换句话说,您根本没有应用任何过滤器。为了能够过滤您的数据,请更改以下代码行:
ptListCollectionRef.getDocuments {/* ... */}
到
query.getDocuments {/* ... */}
关于ios - 无法过滤 Firestore 中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56511003/