【问题标题】:Firebase two-way relationships / Retrieving dataFirebase 双向关系/检索数据
【发布时间】:2016-12-05 13:44:14
【问题描述】:

我想了解有关在 Firebase 中检索数据的一些说明。我不是 NoSql 数据结构方面的专家,我的脑海中缺少一些东西。这对我来说不自然。但是,我想我已经了解了这种结构中双向关系的基础知识。

这是我的数据结构:

{
// 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
      }
    },
    ...
  }
}

我认为每个人对数据结构都有自己的观点,但在我看来(参考 Firebase 文档)它必须是这样的。但是,如果您看到一些更新,请不要犹豫!

因此,在 angularJs 中,我想列出具有“uniqueId1”的帐户的每个“logements”,并显示他们自己的“图像”和 geofire 数据(并检查它们是否是我的用户最喜欢的 logements)。
有可能吗?

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

与此相关的另一个问题:当我删除用户 ID1 的“登录”时,我还想删除所有图像和 geofire 引用...!是否也可以这样做?

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

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

【问题讨论】:

    标签: javascript firebase firebase-realtime-database


    【解决方案1】:

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

    在关系数据库中,您可以使用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;
          if (logementLoadedCount == logementCount) {
            console.log('We've loaded all logements');
          }
        });
      });
    });
    

    许多具有 SQL 和传统 Web 开发背景的开发人员担心所有这些嵌套调用的性能。在不需要的 Firebase 中(对于合理数量的数据),因为 Firebase 通过单个连接对请求进行管道传输。见Speed up fetching posts for my social network app by using query instead of observing a single event repeatedly

    总的来说,我强烈建议您阅读NoSQL data modeling 上的这篇文章,以获得对一般主题的良好介绍。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多