0

First look at the data structure I have created in Firestore:

albums (collection)
    - album_id1 = (doc) 
        year: 1994
        title: "1942: A Love Story"
        uploaded_at: February 23, 1997 at 7:15:52 AM UTC+5:30
        thumbnail: "https:localhost:3000/album-image/1942alovestory"
    - album_id2 = (doc)
        year: 2014
        title: "2 States"
        uploaded_at: July 23, 2018 at 7:15:52 AM UTC+5:30
        thumbnail: "https:localhost:3000/album-image/2states"
    ...

artists (collection)
     - artist_id1 = (doc)
          name: "2 Chainz"
          thumbnail: "https:localhost:3000/artist-image/2chainz"
     - artist_id2 = (doc)
          name: "Arijit Singh"
          thumbnail: "https:localhost:3000/artist-image/arijitsingh"
     ...

genres (collection)
     - genre_id1 = (doc)
          name: "English"
     - genre_id2 = (doc)
          name: "Hindi"

songs (collection)
     - song_id1 = (doc)
         album: (ref) /albums/album_id1
         artists: [
             (ref) /artists/artist_id1,
             (ref) /artists/artist_id2
         ]
         genre: /genres/gerne_id1
         title: "Ek ladki ko dekha"
    ...

Now in this type of data structure, I want to do two things

  • How to read a song with album, artists, and genre data as well in just a single call to Firebase API.
  • Second I want to read 25 songs with album, artists, and genre data, how can I do that?

Please let me know if this is possible with minimum API calls and also let me know if this structure needs to be revised

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
TheWorrier
  • 27
  • 4

1 Answers1

1

How to read a song with album, artists, and genre data as well in just a single call to Firebase API?

There is no way you can perform a single Firestore call and get data from multiple collections at once. Firestore queries are shallow, meaning that they can only return documents from the collection that the query is run against.

To solve this you can either create separate queries that can return the desired data, or you can denormalize the data. This means that you can create another collection that can hold documents that contain all the data you need. In this way, you'll have to query the data only once. Please note that this technique is quite common when it comes to NoSQL databases like Firestore.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193