Firebase - 构建数据库的正确方法 [英] Firebase - proper way to structure the DB

查看:387
本文介绍了Firebase - 构建数据库的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个iOS应用程序,就像一个音乐社交网络。
在应用程序中,用户共享有关特定音乐曲目的帖子。
我想知道在Firebase中构建数据库的最佳方式,因为每个后对象都引用单个track对象。

另外,当用户提交新帖子时,我需要通过查询艺术家+歌曲标题来检查曲目是否已经存在 - 如果曲目不存在,添加一个新曲目。如果轨道存在,请在post对象中引用track_id。

解决方案

和'在Firebase中查询,也不存在。所以你有两块数据在一起,然后做这个查询。这里有一个结构

 艺术家
艺术家_0:Pink Floyd
artist_1:Billy Thorpe
artist_2: Led Zeppelin

tracks
track_id_0:天国的阶梯
track_id_1:太阳的孩子
track_id_2:舒服的麻木

artists_tracks
artist_0_track_id_2:true
artist_1_track_id_1:true
artist_2_track_id_0:true

posts
post_id_0
artist_track:artist_1_track_id_1
post:比利是其中之一内亦读当信息的方范范讯信息
post_id_1
artist_track:artist_0_track_id_2
发布:Floyd是最好的乐队evah。

有了这个结构,如果您知道艺术家和曲目名称,您可以连接它们并做一个简单地查询artists_tracks节点中的.equalToValue(true)以查看它是否存在。



posts节点中的帖子与特定艺术家和曲目相关联。



在某些情况下,您可以将您的数据粘合在一起执行和搜索,而无需额外的节点...像这样

  stuff 
artist_track:Billy_Thorpe_Children_Of_The_Sun

然而,名称中的空格以及文本宽度的变化将不起作用。因此,这将确保您在数据中包含足够的数字,以处理许多歌曲和艺术家,从而保持长度一致。

  artists_tracks 
artist_00000_track_id_00002:true

现在,您可以拥有50,​​000位艺术家和50,000首曲目。


I have an iOS app that is like a social network for music. In the app, users share "posts" about specific music "tracks". I want to know the best way to structure the DB in Firebase, considering that each "post" object references a single "track" object.

Also, when a user submits a new post, I need to check if a track already exists by querying the artist + song title - if the track does not exist, add a new track. If the track exists get the "track_id" to reference in the "post" object.

解决方案

The challenge here is performing an 'and' query in Firebase as well, that doesn't exist. So you have mush two pieces of data together to then do that query. Here's a structure

artists
   artist_0: Pink Floyd
   artist_1: Billy Thorpe
   artist_2: Led Zeppelin

tracks
   track_id_0: Stairway To Heaven
   track_id_1: Children Of The Sun
   track_id_2: Comfortably Numb

artists_tracks
   artist_0_track_id_2: true
   artist_1_track_id_1: true
   artist_2_track_id_0: true

posts
   post_id_0
      artist_track: artist_1_track_id_1
      post: Billy was one of the most creative musicians of modern times.
   post_id_1
      artist_track: artist_0_track_id_2
      post: The Floyd is the best band evah.

With this structure, if you know the artist and the track name, you can concatenate them and do a simple query in the artists_tracks node for .equalToValue(true) to see if it exists.

The posts in the posts node tie back to those specific artists and tracks.

In some cases you can glue your data together to perform and searches without the extra nodes... like this

stuff
  artist_track: Billy_Thorpe_Children_Of_The_Sun

However, because of the spaces in the names and the varying width of the text it won't work. So that leads to ensuring you include enough digits in the data to handle however many songs and artists so the length stays consistent.

artists_tracks
   artist_00000_track_id_00002: true

Now you can have 50,000 artists and 50,000 tracks.

这篇关于Firebase - 构建数据库的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆