Search code examples
swiftfirebasefirebase-realtime-databaseswift4google-cloud-firestore

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


Console Image

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!


Solution

  • 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)");
        }
    }