1

我的 Firebase 阵列具有以下结构:

[
   {
  'userId': '12345',
  'itemOrdered' : 'abc',
  'status': 'pending'
   ...other attributes
   },
  {
  'userId': '6789',
  'itemOrdered' : 'def',
  'status' : 'pending',
   ...other attributes
   },
  {
  'userId': '12345',
  'itemOrdered' : 'def',
  'status' : 'complete',
   ...other attributes
   },

]  

我无法弄清楚如何检索以下数据:

  1. 获取 userId = xxx 的记录
  2. 获取 'itemOrdered' = 'def' 的所有记录

Firebase 文档谈论使用 orderByChild 但这没有多大意义。

4

2 回答 2

2

假设您使用 JavaScript SDK 访问 Firebase:

  1. ref.orderByChild('userId').equalTo('xxx')

  2. ref.orderByChild('itemOrdered').equalTo('def')

如果您正在尝试构建一个def从 user获取项目顺序的查询xxx,那么 Firebase 的查询目前无法实现。查询多个属性值的唯一方法是以允许查询的方式将它们组合在一个属性中。例如

  1. ref.orderByChild('userId_itemOrdered').equalTo('xxx_def')
于 2015-06-22T17:40:13.800 回答
0

根据您的平台,还有其他选项。

1) Query for the userId and then filter in code for the item you are looking for. 

2) Query for the userId and build another query based on those results.

3) Flatten your data!

例如:创建一个名为 user_purchases 的节点,每个子节点都可以是一个 userId 节点,其中用户购买了 itemId(这使得知道用户购买了哪些商品非常容易)。或者创建一个 items_purchased 节点,每个子节点都是一个项目编号节点,然后关联购买该项目的用户 ID。

于 2015-06-22T21:01:47.160 回答