ios - 循环通过 Firestore 数据库
问题描述
我希望循环通过我的 Firestore DB 以获取与我的所有文档组合的总距离
我目前正在使用此查询来获取文档总数
firebaseDB.collection("journey").document(key).collection("journeys")
.getDocuments() { (querySnapshot, err) in
if let err = err {
print("Error getting documents: \(err)")
}
else
{
var totalJourney = 0
for document in querySnapshot!.documents {
totalJourney += 1
print("\(document.documentID) => \(document.data())");
}
print("totalJourney = \(totalJourney)");
self.totalJourney.text = String(totalJourney)
}
}
解决方案
您的代码并不遥远,但您的代码中缺少一些东西 - 例如,读取每个节点中的距离子节点并将它们相加。下面是一些示例代码,用于输出行程数和总里程数。
我的结构比你的略浅,我使用了一个名为“英里”的子节点而不是距离,但概念是一样的。
func readJourneys() {
self.db.collection("journeys").getDocuments() { (querySnapshot, err) in
if let err = err {
print("Error getting documents: \(err)")
} else {
let count = querySnapshot!.documents.count
var totalMiles = 0
for document in querySnapshot!.documents {
//let journeyId = document.documentID
let miles = document.get("miles") as! Int
totalMiles += miles
}
print("There were: \(count) journeys covering: \(totalMiles) miles")
}
}
}
输出是
There were: 3 journeys covering: 20 miles
推荐阅读
- typescript - 如何获取自定义TypeMap函数返回的默认类型
- javascript - 如何在数组中有一个可以根据编辑进行编辑的数字(删除或添加)
- java - Java,比较 ArrayList 的值
- django - 登录页面出错 - TypeError: __init__() got an unexpected keyword argument 'request'
- jquery - Vue中如何重复移动一个元素?
- reactjs - 列出文件以使用列表生成动态组件
- sf - 使用 WifiManager 和 Arduinojson 6 保存自定义参数
- graphql - 用于从 GraphQL 端点定期镜像完整数据集的工具?
- sql - 在 Neteeza 中使用 LIKE 运算符加入
- postscript - 在 postscript 中,如何从当前活动的字体中获取 UnderlinePosition,它是一个整数?