【问题标题】:Filter firebase records by the array value of a specific property按特定属性的数组值过滤 Firebase 记录
【发布时间】:2019-04-15 23:49:26
【问题描述】:

我在玩firebase functions,我有一个问题。我的firebase DB的结构如下:

 myDb
  |
Users
    |
    000001
      |
      Name: Sam
      |
      Items: [1,2,3]
    |
    000002
      |
      Name: Jim
      |
      Items: [3,4,5]

我想要做的是过滤所有在其 Items 值中具有 3 的用户。我在他们用于过滤数据的文档中没有找到类似于:this SO post 的任何内容。也许是因为它不是一个简单的值。我不知道。我现在想知道如何执行这个特定的过滤。

【问题讨论】:

  • 如何存储数组?元素的索引是关键属性吗:items: { 0: 1, 1: 2, 2:3 }

标签: firebase react-native google-cloud-functions


【解决方案1】:

在 Firebase 实时数据库中无法使用数组包含过滤器,但它是最近添加到 Firestore 的。

firebase.firestore().collection('users').where('items', 'array-contains', 3)

在 Firebase 实时数据库中,您可能希望将它们构建为可以查询的对象

{
    name: "Jim",
    items: {
        1: true,
        2: true,
        3: true
    }
}

然后查询会是这样的

firebase.database().ref('users').orderByChild('items/3').equalTo(true);

【讨论】:

  • 是否有可能将键作为字符串(例如 {"str1":1, "str2":2})然后有 .orderByChild('items/str1').equalTo( 1); ?
猜你喜欢
  • 1970-01-01
  • 2020-10-12
  • 1970-01-01
  • 2021-08-05
  • 2019-05-30
  • 1970-01-01
  • 2021-12-08
  • 2019-05-04
  • 1970-01-01
相关资源
最近更新 更多