Firebase双向关系/检索数据 [英] Firebase two-way relationships / Retrieving data

查看:192
本文介绍了Firebase双向关系/检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对firebase中的数据检索进行一些解释。我不是NoSql数据结构方面的专家,在我的脑海里却缺少一些东西。这对我来说并不自然。不过,我想我已经理解了这种结构中双向关系的基础知识。


这是我的数据结构:

  {
//帐户和帐户之间的双向关系logements
accounts:{
uniqueId1:{
nom:'我的名字1',
email:'我的电子邮件1',
logements:{
uniqueIdLogement1:true,
//我是否必须列出关系图像& geofire在这里?
uniqueIdLogement2:true
},
favorites:{
uniqueIdLogement2:true,
uniqueIdLogement25:true,
uniqueIdLogement32:true
}

},
....
},
//每个logement都有自己的images& geofire数据
logements:{
uniqueIdLogement1:{
nom_du_logement:'我的名字1',
accounts:{
uniqueId1:true
},
images:{
uniqueIdImages1:true,
uniqueIdImages2:true,
uniqueIdImages3:true,
},
geofire:{
uniqueIdGeofire1:true
},
},
...
},
images:{
uniqueIdImages1:{
image:'我的图片网址1',
logements:{
uniqueIdLogement1:true
}
},$
geofire:{
uniqueIdGeofire1:{
g:'我的geofire数据,
l :{
0:'-44.34',
1:'-3.2'
},
logements:{
uniqueIdLogement1:true
}
},
...
}
}

我认为每个人对于数据结构都有自己的观点,但在我看来(参考Firebase Docs),它必须是相当类似的。但是,如果你看到一些更新,不要犹豫!


所以,在angularJs中,我想以uniqueId1列出每个logements作为例子,并显示自己的images & geofire数据(并检查它们是否是我的用户的最爱logements)。
是否有可能做到这一点?

  //列出每一个帐户ID1 
//每一个logements对于每个logement拍摄图像数据& geofire数据&检查收藏夹
//在$ scope.items&中推送信息用ng-repeat显示

另一个相对的问题是:当我删除一个logements对于用户ID1,我想删除所有图像& geofire参考...!


  //从账户ID 1 $ b中删除uniqueIdLogement1 $ b //删除其中logements= uniqueIdLogement1 
的图片//删除goofier其中logements= uniqueIdLogement1

我认为,如果我理解正确,那对我来说就没问题了!我现在看不到它是如何工作的,这是令人沮丧的,因为我知道这种数据库有很大的潜力。你能解释一下我的一些细节吗?非常感谢您
解决方案

使用任何数据库,您通常需要连接多个位置的数据来构建视图。

在关系型数据库中,您可以使用一个语句从这些多个位置(在这种情况下的表格)中获取数据,方法是使用 JOIN 子句。



在Firebase(和许多其他NoSQL数据库)中,没有内置的方法可以从多个位置连接数据。所以你必须在你的代码中这样做。

  var ref = firebase.database()。ref(); 
var accountRef = ref.child('accounts / uniqueId1');
accountRef.on('value',function(accountSnapshot){
var logementCount = accountSnapshot.child('logements')。numChildren;
var logementLoadedCount = 0;
accountSnapshot。 child('logements')。forEach(function(logementKey){
var logementRef = ref.child('logements')。child(logementKey.key);
logementRef.once('value',function (logementSnapshot){
var logement = logementSnapshot.val();
logementLoadedCount = logementLoadedCount + 1; $ b $ if(logementLoadedCount == logementCount){
console.log('We'已加载所有的logements');
}
});
});
});

来自SQL和传统Web开发背景的许多开发人员担心所有这些嵌套调用的性能。在不需要的Firebase中(对于合理数量的数据),因为Firebase通过单个连接管理请求。请参阅加快取回我的社交网络应用程序的帖子,通过使用查询,而不是重复观察一个单一的事件

一般来说,我强烈建议阅读本文在 NoSQL数据建模中有一个很好的介绍一般的话题。

I would like some explanations about data retrieving in firebase. I'm not an expert in NoSql data structure and something is missing in my mind. It's not natural for me. However, I think i've understood the basics of Two-way relationships in this kind of structure.

Here is my data structure :

{
// Two-way relationships between accounts & logements
  "accounts" : {
    "uniqueId1" : {
      "nom" : 'My name 1',
      "email" : 'My email 1',
      "logements" : {
          uniqueIdLogement1 : true,
          // do I have to list relationships with images & geofire here ?
          uniqueIdLogement2 : true
      },
      "favorites" : {
          uniqueIdLogement2 : true,
          uniqueIdLogement25 : true,
          uniqueIdLogement32 : true
      }

    },
    ....
  },
  // Each logement has his own "images" & "geofire" data
  "logements" : {
    "uniqueIdLogement1" : {
      "nom_du_logement" : 'My logement name 1',
      "accounts" : {
          uniqueId1 : true
      },
      "images" : {
          uniqueIdImages1 : true,
          uniqueIdImages2 : true,
          uniqueIdImages3 : true,
      },
      "geofire" : {
          uniqueIdGeofire1 : true
      },
    },
    ...
  },
  "images" : {
    "uniqueIdImages1" : {
      "image" : 'My image URL 1',
      "logements" : {
          uniqueIdLogement1 : true
      }
    },
    ...
  },
  "geofire" : {
    "uniqueIdGeofire1" : {
      "g" : 'My geofire Data,
      "l" : {
          0 : '-44.34',
          1 : '-3.2'
      },
      "logements" : {
          uniqueIdLogement1 : true
      }
    },
    ...
  }
}

I think everyone has his own point of view about data structure but in my opinion (in reference of Firebase Docs) it has to be quite like that. But if you see some updates, don't hesitate !

So, In angularJs, i would like to list each "logements" for account with "uniqueId1" for exemple and display their own "images" & geofire data (and check if they are my user's favorites logements).
Is it possible to do that ?

// list every logements for account ID1
    // for each logement take images data & geofire data & check if favorites
        // push infos in $scope.items & display with ng-repeat

Another question relative to that : When i remove a "logements" for User ID1, i want to remove also all images & geofire references ... ! Is it possible to do that too ?

// remove uniqueIdLogement1 from account ID 1
   // remove images where "logements" = uniqueIdLogement1
   // remove goofier where "logements" = uniqueIdLogement1

I think that if i understand that correctly, it will be okay for me ! I just can't see right now how it works and it's frustrating because i know there is a lot of potential with this kind of database. Can you explain me please some details about that . Thank you very much

解决方案

With any database you'll often need to join data from multiple locations to build your view.

In relational databases, you can get the data from these multiple locations (tables in that case) with a single statement, by using a JOIN clause.

In Firebase (and many other NoSQL databases) there is no built-in way to join data from multiple locations. So you will have to do that in your code.

var ref = firebase.database().ref();
var accountRef = ref.child('accounts/uniqueId1');
accountRef.on('value', function(accountSnapshot) {
  var logementCount = accountSnapshot.child('logements').numChildren;
  var logementLoadedCount = 0;
  accountSnapshot.child('logements').forEach(function(logementKey) {
    var logementRef = ref.child('logements').child(logementKey.key);
    logementRef.once('value', function(logementSnapshot) {
      var logement = logementSnapshot.val();
      logementLoadedCount = logementLoadedCount + 1;
      if (logementLoadedCount == logementCount) {
        console.log('We've loaded all logements');
      }
    });
  });
});

Many developers from a SQL and traditional web development background worry about the performance of all those nested calls. In Firebase that is not needed (for reasonable amounts of data), since Firebase pipelines the requests over a single connection. See Speed up fetching posts for my social network app by using query instead of observing a single event repeatedly.

In general I highly recommend reading this article on NoSQL data modeling for a good introduction to the general topic.

这篇关于Firebase双向关系/检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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