Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Firestore subcollections

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?

like image 853
Vlad78 Avatar asked Sep 13 '18 07:09

Vlad78


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

like image 74
DionizB Avatar answered Sep 28 '22 04:09

DionizB