繁体   English   中英

如何从 Firestore 快速从集合中获取文档 ID

[英]how to get documents id's from a collection with swift from firestore

我想从 firestore 获取文档的 id 并将它们存储在一个变量中以在我的下一页中使用。

我怎么能用 swift 做到这一点? 我尝试了这种方法,但它生成了一个随机 ID,而不是我拥有的集合的特定文档 ID

let docRef = Firestore.firestore().collection("Shops").document()
let docId = docRef.documentID

附件是我需要在我的变量中检索的文档 ID。

这些是我需要在我的变量中检索的 ID

您可以使用getDocuments()获取一个集合中的所有文档,或使用.addSnapshotListener()自动获取新文档。

Firestore.firestore().collection("Shops").getDocuments() { (querySnapshot, err) in
    if let err = err {
        print("Error getting documents: \(err)")
    } else {
        for document in querySnapshot!.documents {
            print("\(document.documentID)") // Get documentID
            print("\(document.data)") // Get all data
            print("\(document.data()["name"] as! String)") // Get specific data & type cast it.
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM