Get Firestore subcollections

Viewed 19726

I have a problem with getting data from the Firestore with a following structure:

Firestore structure

Here is how I get category collection:

var defaultStore: Firestore?
var location: [DocumentSnapshot] = []

override func viewDidLoad() {
    super.viewDidLoad()
    defaultStore = Firestore.firestore()

    defaultStore?.collection("Category").getDocuments { (querySnapshot: QuerySnapshot?, error: Error?) in
        if let error = error {
            print(error.localizedDescription)
        } else {
            self.location = (querySnapshot?.documents)!
            DispatchQueue.main.async {
                self.tableView.reloadData()
            }
        }
    }
}

It gives me: Books, Films, From test and Serials. But how can I get collections from Films for example?

1 Answers

You can try

defaultStore?.collection("Category").document("Film").collection("firstFilm").getDocuments();

Because Films, Books etc are documents not collections as you can see. For more info read here Firestore data model

Related