根据Firebase中的类别检索数据 [英] Retrieve data based on categories in Firebase

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

问题描述

我是Firebase的新成员,并且有关于检索某些数据的问题。
比方说,我有以下数据结构:



我的查询的目标是给定的国家/地区名称,我想检索其目标数组包含此国家/地区的所有用户。例如:
用于查询Jordan,它将检索:user2和user3。



有人知道如何用Firbase Angular做这样的查询吗?

解决方案

Firebase没有与数组中的项匹配的查询运算符。事实上:Firebase建议不要使用阵列来处理您所遇到的情况。而是像这样存储数据:

 顾问国家:{
user1:{
destinations: {
安哥拉:真,
澳大利亚:真
}
},
用户2:{
目的地:{
安哥拉:真,
奥地利:真实,
约旦:真实
}
},
用户3:{
目的地:{
奥地利:真实
埃及:真实的,
Jordan:真实的
}
}
}

现在您可以查询具有目的地Jordan的用户:

  var users = ref.child('advisors-国家')。orderByChild('destinations / Jordan')
.equalTo(true);

然后将这个绑定到您的AngularJS作用域:

  $ scope.users = $ firebaseArray(users); 

这种类型的数据结构称为索引,您经常会发现需要添加特定的索引到您的NoSQL数据库,以适应您的应用程序的查询要求。看到这个关于NoSQL数据建模的文章为好介绍这个话题。

I'm new with Firebase and I have a question about retrieving some data. Lets say I'm having the following data structure:

The goal of my query is for a given country name I want to retrieve all the users that their destinations array contain this country. For example: for the query of "Jordan" it will retrieve: user2 and user3.

Anybody know how can I do such a query with Firbase Angular?

解决方案

Firebase doesn't have a query operator that matches items in an array. In fact: Firebase recommends against using arrays for situations like the one you have. Instead, store the data like this:

advisors-countries: {
  user1: {
    destinations: {
      Angola: true,
      Australia: true
    }
  },
  user2: {
    destinations: {
      Angola: true,
      Austria: true,
      Jordan: true
    }
  },
  user3: {
    destinations: {
      Austria: true
      Egypt: true,
      Jordan: true
    }
  }
}

Now you can query users that have destination Jordan with:

var users = ref.child('advisors-countries').orderByChild('destinations/Jordan')
                                           .equalTo(true);

And then bind this to your AngularJS scope with:

$scope.users = $firebaseArray(users);

This type of data structure is called an index and you'll often find you need to add specific indexes to your NoSQL database to fit the querying requirements of your app. See this article on NoSQL data modeling for a good introduction on the topic.

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

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