FireStore Swift 4 : How to get total count of all the documents inside a collection, and get details of each document?

我们两清 提交于 2019-12-22 13:36:06

问题


I need to get all the users count from superuser and list those in a table view with there details. Is there a code to directly get the count of documents inside a collection, other than using functions inside firebase console. Or a Simple Query to traverse through the documents!


回答1:


this will collect all the document for a collection and print them

db.collection("superUsers").getDocuments() 
{ 
    (querySnapshot, err) in

    if let err = err 
    {
        print("Error getting documents: \(err)");
    } 
    else 
    {
        var count = 0
        for document in querySnapshot!.documents {
            count += 1
            print("\(document.documentID) => \(document.data())");
        }

        print("Count = \(count)");
    }
}



回答2:


You can count the documents in a snapshot:

snapshot.count


来源:https://stackoverflow.com/questions/48516763/firestore-swift-4-how-to-get-total-count-of-all-the-documents-inside-a-collect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!