Search code examples
ionic-frameworkgoogle-cloud-firestoreangularfire2ionic4

get nested collection data with child id only in Firestore


I'm using Ionic 4 with Firestore and using AngularFire2

I have nested collection in the Firestore database

|- service_states                       # Collection
  | collection_id: 12345
  | name: Delhi
    |- service_cities                   # Collection
       | collection_id: 23456
       | name: Delhi
         |- service_area                # Collection
            | collection_id: 3456
            | name: Area 1

I have another independent collection

|- profile
   | user_id: 8765
   | service_area: 3456

I'm able to get the service_area details, If I have service_states and service_cities data like

this.profileCollection = this.aFS.collection('service_states/12345/service_cities/23456/service_ares/3456');

But in this case, I don't have service_area id and service_states id. How can I get the details of service_area with only service_area id?


Solution

  • If you can't build the full path to a collection, including any necessary intermediate document IDs, then it's not possible to do anything with that collection. You must be able to find a collection to query it.

    If you don't know anything about a document except some value of a field contained in the document, you'll need to query a top-level collection to get it. So, perhaps you need a new top-level collection to contain all the service areas.