每个Firestore文档中都有两个数组。在我的应用中,您可以基于这些数组中的值进行搜索。如果我根据选择的选项在创建的数组之间查找完全匹配,然后在这样的文档中查找完全匹配,我就能使搜索工作正常。
let _ = circleQuery.observe(.documentEntered, with: { (key, location) in
if let key = key {
Constants.firestore.collection("facilities").whereField("facility_type", isEqualTo: self.type!)
.whereField("insurance_accepted", isEqualTo: self.insurance!).getDocuments { (querySnapshot, error) in
if let err = error {
print(err.localizedDescription)
} else {
for document in (querySnapshot?.documents)! {
if document.documentID == key {
self.facilities?.append(document.data())
self.addFaciltiesToMap()
}
}
}
}
}
})
这个问题是,这不是在看单个值。我想基于两个文档数组中是否包含本地数组中的值来显示结果。用户具有可以从每个阵列中选择的选项,而不必从两个阵列中选择,因此他们可以选择仅包含在一个或另一个中的选项。例如,他们可以从“ facility_type”数组中选择0到4个选项,并从“ insurance_accepted”数组中选择0到2个选项。因此,如果他们仅从“ facility_type”中选择选项,我只想在该文档数组中查找选定的值,“ insurance_accepted”文档数组也是如此。如果他们从两个选项中都选择了一个,那么我需要在两个中都选择一个选项。如果它们只是从一个或另一个中进行选择,则需要根据其选择的值是否存在于文档中的那个数组中来返回结果。如果选择了两者,则我需要根据是否在每个文档数组中至少进行一次匹配来返回结果。
这基本上就是我要实现的目标。
for value in self.type! {
Constants.firestore.collection("facilities").whereField("facility_type", arrayContains: value).getDocuments { (querySnapshot, error) in
if let err = error {
print(err.localizedDescription)
} else {
for document in (querySnapshot?.documents)! {
if document.documentID == key {
self.facilities?.append(document.data())
}
}
}
}
}
for value in self.insurance! {
Constants.firestore.collection("facilities").whereField("insurance_accepted", arrayContains: value).getDocuments { (querySnapshot, error) in
if let err = error {
print(err.localizedDescription)
} else {
for document in (querySnapshot?.documents)! {
if document.documentID == key {
self.facilities?.append(document.data())
}
}
}
}
}