Get Firestore subcollections

19,202

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

Share:
19,202
Vlad78
Author by

Vlad78

Updated on June 07, 2022

Comments

  • Vlad78
    Vlad78 about 2 years

    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?

  • Alexander
    Alexander over 4 years
    also, we can use fhttps://firebaseopensource.com/projects/firebase/firebase-a‌​ndroid-sdk/docs/ktx/‌​firestore.md/
  • EngineSense
    EngineSense over 3 years
    This is really hard when it's come to using random id, You cannot simply get the collection of it's subcollection in method like the above. You need to loop it get it. But at final hard to implement.