【问题标题】:Search by a value in Firebase在 Firebase 中按值搜索
【发布时间】:2021-09-09 22:26:59
【问题描述】:

这是我在 firebase 中的数据:

User:
|--0:
|--|--name: Alex
|--|--Age: 40
|--|--Weapons:
|--|--|--:axe: Axe
|--|--|--:sword: Sword
|--1:
|--|--name: John
|--|--Age: 22
|--|--Weapons:
|--|--|--:gun: Gun
|--2:
|--|--name: Mary
|--|--Age: 28
|--|--Weapons:
|--|--|--:shield: Shield
|--|--|--:sword: Sword
|

如何搜索使用“剑”的用户?
我的 nodeJS API 需要这样的结果:

[{"name":"Alex","Age":40,"Weapons":{"axe":"Axe","sword":"Sword"}},    
{"name":"Mary","Age":28,"Weapons":{"shield":"Shield","sword":"Sword"}}]

感谢大家的帮助

【问题讨论】:

    标签: node.js firebase firebase-realtime-database


    【解决方案1】:

    您可以通过对子属性的简单查询来做到这一点:

    ref.child("User").orderByChild("Weapons/sword").equalTo("Sword")
    

    ref.child("User").orderByChild("Weapons/Sword").equalTo(True)
    

    只要确保您也在安全规则中索引值:

    {
      "rules": {
        "Users": {
          "$userIndex": {
            "Weapons": {
              ".indexOn": ["Sword", "Shield", "Gun"]
            }
          }
        }
      }
    }
    

    【讨论】:

    • 谢谢你,我用你的武器/剑解决了。实际上,我的规则也适用于:"rules": { "Users": { ".indexOn": ["name", "age", "Weapons/axe", "Weapons/shield", "Weapons/sword", "Weapons/gun"] }, }
    • 听起来不错,我不熟悉您的项目,我发布的示例可以更好地适应多个领域 - 但只要它有效:)
    猜你喜欢
    • 2017-03-10
    • 2017-03-21
    • 1970-01-01
    • 2017-06-29
    • 1970-01-01
    • 2016-04-04
    • 2017-10-31
    • 2019-09-25
    相关资源
    最近更新 更多