简体   繁体   English

如何使用查询或特定路径从 Firebase 实时数据库中高效查询

[英]How to efficiently query from Firebase Realtime Database using query or specific path

We have infrequently property modification("price") on Firebase Realtime Database which is structure like this:我们很少对 Firebase 实时数据库进行属性修改(“价格”),其结构如下:

../currencies/<currency>/value/
      "price":343

every player that log in will listen to only one and specific currency.每个登录的玩家都只会收听一种特定的货币。 our client will pick the right currency path based on the player preferences.我们的客户会根据玩家的喜好选择正确的货币路径。

so if player is set to currency USD the firebase client will listen to this path因此,如果播放器设置为货币 USD,则 firebase 客户端将监听此路径

../currencies/USD/value/
      "price":343

Those currencies prices are changed infrequently这些货币价格不经常变动
due to this structure our server side need to modify and denormalized the data to all currencies when it changes (we can have ten's of currencies) because of that we add to the leaf even more properties which are identical at all currencies由于这种结构,我们的服务器端需要在数据发生变化时将数据修改和非规范化为所有货币(我们可以有十种货币)因此我们向叶子添加了更多在所有货币上都相同的属性

which I find redudent like:我发现它像:

../currencies/USD/value/
          "price":343
          "currency-source":"fx" . //this property will be copied to all 

currencies vals because the client listen to only one path and it needs this data aswell currency vals,因为客户端只听一条路径,它也需要这些数据

instead if manitain this on path's perhaps we can use some query where each client will be able to pick it's currency based on property name?相反,如果在路径上维护这个,也许我们可以使用一些查询,每个客户都可以根据属性名称选择它的货币?

something like that:是这样的:

../currencies/value/
          "USD_price":343
          "EUR_price":343
      ...

thoughts about design?关于设计的想法? and if sounds better how can be achieved with Firebase Realtime Database query?如果听起来更好,如何使用 Firebase 实时数据库查询来实现?

I don't know which language (which Client SDK) you use in your app, but here is a solution with the JavaScript SDK.我不知道您在应用程序中使用哪种语言(哪个客户端 SDK),但这里有一个使用 JavaScript SDK 的解决方案。

Let's imagine you have a Relatime Database structure as follows假设您有一个 Relatime 数据库结构,如下所示

  "parentnode" : {
    "currencies" : {
      "EUR" : {
        "value" : {
          "price" : 201
        }
      },
      "USD" : {
        "value" : {
          "price" : 343
        }
      },
      "value" : {
        "EUR_price" : 201,
        "USD_price" : 343,
        "currency-source" : "fx"
      }
    }
  }

Under a parentnode you have a currencies node which corresponds to the examples in your question .parentnode节点下,您有一个currencies节点对应于您问题中的示例

In case you want to listen to /currencies/<currency>/value , you would do as follows:如果你想听/currencies/<currency>/value ,你会做如下:

  var db = firebase.database();

  var currency = 'EUR';

  var ref = db.ref().child('parentnode/currencies/' + currency);
  ref.on('value', function(snapshot) {
    var data = snapshot.val();
    console.log(data);
  });

In case you want to listen to /currencies/value/<currency>_price and get the price and the currency-source values, you would do as follows:如果您想收听/currencies/value/<currency>_price并获取pricecurrency-source值,您可以执行以下操作:

  var db = firebase.database();

  var currency = 'EUR';

  var ref = db.ref().child('parentnode/currencies/value');
  ref.on('value', function(snapshot) {
    var data = snapshot.val();
    var price = data[currency + '_price'];
    var source = data['currency-source'];
    console.log(price);
    console.log(source);
  });

As mentioned in your comment, the second approach implies that "we will download all the data leaf under /currencies/value/ ".正如您在评论中提到的,第二种方法意味着“我们将下载/currencies/value/下的所有数据叶”。

I can think of two other possible approaches.我可以想到另外两种可能的方法。 Choosing one over the other really depends on your functional requirements, ie what you do with these values in your front-end.选择一个而不是另一个实际上取决于您的功能需求,即您在前端如何处理这些值。

1/ Set two listeners 1/ 设置两个监听器

The idea is to set one listener for 'parentnode/currencies/value/' + currency + '_price' and one for 'parentnode/currencies/value/currency-source' , as follows:这个想法是为'parentnode/currencies/value/' + currency + '_price'设置一个监听器,为'parentnode/currencies/value/currency-source'设置一个监听器,如下所示:

  var currency = 'EUR';

  var ref2 = db
    .ref()
    .child('parentnode/currencies/value/' + currency + '_price');
  ref2.on('value', function(snapshot) {
    var data = snapshot.val();
    console.log(data);
  });

  var ref3 = db.ref().child('parentnode/currencies/value/currency-source');
  ref3.on('value', function(snapshot) {
    var data = snapshot.val();
    console.log(data);
  });

2/ Query the currency-source value within the listener 2/ 查询监听器内的currency-source

With this second approach, in the listener to 'parentnode/currencies/value/' + currency + '_price' , we query the database with the once() method to get the value of currency-source :使用第二种方法,在'parentnode/currencies/value/' + currency + '_price'中,我们使用once()方法查询数据库以获取currency-source的值:

  var ref4 = db
    .ref()
    .child('parentnode/currencies/value/' + currency + '_price');
  ref4.on('value', function(snapshot) {
    var data = snapshot.val();
    console.log(data);
    db.ref()
      .child('parentnode/currencies/value/currency-source')
      .once('value')
      .then(function(dataSnapshot) {
        console.log(dataSnapshot.val());
      });
  });

Note that if you do not need to set a listener, ie you want to fetch the data once (eg by triggering the fetch from a button, or on page loading, etc..) you should only use the once() method and then you can chain the two calls as follows:请注意,如果您不需要设置侦听器,即您只想获取一次数据(例如,通过从按钮触发获取,或在页面加载时等),您应该只使用once()方法,然后您可以按如下方式链接这两个调用:

  var currency = 'EUR';
  var ref5 = db.ref().child('parentnode/currencies/value/' + currency + '_price');
  var ref6 = db.ref().child('parentnode/currencies/value/currency-source');

  ref5
    .once('value')
    .then(function(dataSnapshot) {
      console.log(dataSnapshot.val());
      return ref6.once('value');
    })
    .then(function(dataSnapshot) {
      console.log(dataSnapshot.val());
    });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 哪个是使用 firebase 实时数据库更好的性能查询 - Which is a better query for performance using firebase realtime-database Firebase 实时数据库查询最近 10 条记录使用时间戳键 - Firebase realtime database query latest 10 records using timestamp key 如何查询firebase实时数据库获取上月数据 - How to query firebase realtime database to get last month data 如何使用 Flutter/Dart 从 Firebase 实时数据库中读取数据 - How to read data from Firebase Realtime database using Flutter/Dart 为什么我对 Firebase 实时数据库的读取查询这么慢? - Why is my read query to Firebase Realtime Database so slow? 从 Node.js 中的 Firebase 实时数据库中选择特定属性 - Select specific properties from Firebase Realtime Database in Node.js 如何使用实时数据库“Flutter”中的 id 从根 ref 的其他子项查询和显示 listview.builder 中的数据? - How to query and display data in listview.builder from other child of the root ref, using id in realtime database "Flutter"? 如何在 Firebase 实时数据库中使用 firebase 唯一 uid 放置 UserDetails - How to put UserDeatils using firebase unique uid in Firebase Realtime Database 如何使用 Firebase 9(模块化 sdk)更新 Firebase 实时数据库中的数据 - How to update data in Firebase realtime database using Firebase 9 (modular sdk) 如何在 React Native 应用程序中从 Firebase 实时数据库中获取数据 - How to Fetch Data from Firebase Realtime Database in React Native App
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM