如何在一次 API 调用中从 Firebase 文档中读取引用的数据?

huangapple go评论56阅读模式
英文:

How to read referenced data from docs in firebase in one call of api?

问题

请查看我在Firestore中创建的数据结构:

albums(集合)
    - album_id1 =(文档)
        year: 1994
        title: "1942: A Love Story"
        uploaded_at: 1997年2月23日 上午7:15:52 UTC+5:30
        thumbnail: "https:localhost:3000/album-image/1942alovestory"
    - album_id2 =(文档)
        year: 2014
        title: "2 States"
        uploaded_at: 2018年7月23日 上午7:15:52 UTC+5:30
        thumbnail: "https:localhost:3000/album-image/2states"
    ...

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

genres(集合)
    - genre_id1 =(文档)
        name: "English"
    - genre_id2 =(文档)
        name: "Hindi"

songs(集合)
    - song_id1 =(文档)
        album:(引用)/albums/album_id1
        artists: [
            (引用)/artists/artist_id1,
            (引用)/artists/artist_id2
        ]
        genre: /genres/gerne_id1
        title: "Ek ladki ko dekha"
    ...

现在在这种数据结构中,我想要做两件事:

  • 如何在单个调用Firebase API中读取带有专辑、艺术家和流派数据的歌曲。
  • 其次,我想读取带有专辑、艺术家和流派数据的25首歌曲,该如何操作?

请告诉我是否可以通过最少的API调用来实现,并且如果需要修改此结构,请告诉我。

英文:

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

答案1

得分: 1

无法一次性调用Firebase API来读取带有专辑、艺术家和流派数据的歌曲。Firestore 查询是浅层的,意味着它们只能返回针对查询所在的集合的文档。

为了解决这个问题,你可以创建单独的查询来获取所需的数据,或者你可以对数据进行去规范化处理。这意味着你可以创建另一个集合,其中包含包含所需所有数据的文档。这样,你只需查询数据一次。请注意,这种技术在像Firestore这样的NoSQL数据库中相当常见。

英文:

> 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.

huangapple
  • 本文由 发表于 2023年7月17日 16:45:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76702790.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定