Firebase查询 - 嵌套数据 [英] Firebase Query - Nested Data

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

问题描述

我试图在firebase中查询一些分层数据。

  {
orgs)我有点困难了解如何查询以下数据结构: :{
-KBFXBBEyvgtfqMvU4pi:{
name:ACME 123,
owner:-K9IPqIUIuEFzLS0f_Pe,
users:{
-KBF_GhwTmXfR6Jce30t:{
email:userA@company.com,
permission:editor,
userKey:K99LV9cTjh1ovW1D5j2
},
-KBF_M533zzbUilGvAAW:{
email:userB@company.com,
permission:editor
}
}
},
-KBFaKlJ8tfqjBQjAZgq:{
name:ACME Alt LLC,
owner:-K9IPqIUIuEFzLS0f_ZZ,
users: {
-KBFbD4trt9nyeHPUQbn:{
email:userX@co.com,
permission:editor
}
}





特别是我想知道如果电子邮件地址userB@company.com存在。但是这对我来说有点困惑,因为我需要通过2级(orgs和users)搜索。

更多的文档,看来我真的不应该像这样嵌套我的数据。我会坦白的说,在使用层次结构的JSON模式时,看起来有点相反。无论如何,这就是我现在要做的:

  {
orgs:{
-KBFXBBEyvgtfqMvU4pi:{
name:ACME 123,
owner:-K9IPqIUIuEFzLS0f_Pe
}
},
-KBFaKlJ8tfqjBQjAZgq :{
name:ACME Alt LLC,
owner:-K9IPqIUIuEFzLS0f_ZZ
}
}
},
orgMembership:{
-KBFXBBEyvgtfq7h381h:{
org:-KBFXBBEyvgtfqMvU4pi,
email:userA@company.com,
permission :editor
}
}

然后我可以使用下面的查询:

  orgMRef.orderByChild(email)。equalTo(userA@company.com)。once(child_added ,函数(快照){
console.log(found:+ snapshot.key());
});


I am attempting to query some hierarchical data in firebase. I'm having a little difficulty in figuring out how to query the following data structure:

{
  "orgs": {
    "-KBFXBBEyvgtfqMvU4pi": {
      "name": "ACME 123",
      "owner": "-K9IPqIUIuEFzLS0f_Pe",
      "users": {
        "-KBF_GhwTmXfR6Jce30t": {
          "email": "userA@company.com",
          "permission": "editor",
          "userKey": "K99LV9cTjh1ovW1D5j2"
        },
        "-KBF_M533zzbUilGvAAW": {
          "email": "userB@company.com",
          "permission": "editor"
        }
      }
    },
    "-KBFaKlJ8tfqjBQjAZgq": {
      "name": "ACME Alt LLC",
      "owner": "-K9IPqIUIuEFzLS0f_ZZ",
      "users": {
        "-KBFbD4trt9nyeHPUQbn": {
          "email": "userX@co.com",
          "permission": "editor"
        }
      }
    }
  }
}

Specifically, I would like to find out if the email address "userB@company.com" exists. But this is a little confusing for me since I need to search through 2 levels (orgs and users).

解决方案

After reading some more documentation it seems I really shouldn't be nesting my data like this. I'll be honest, it seems kind of contrary to do this when working with a JSON schema that that is hierarchical. Anyway, this is what I'm looking to do now:

{
  "orgs": {
    "-KBFXBBEyvgtfqMvU4pi": {
      "name": "ACME 123",
      "owner": "-K9IPqIUIuEFzLS0f_Pe"
      }
    },
    "-KBFaKlJ8tfqjBQjAZgq": {
      "name": "ACME Alt LLC",
      "owner": "-K9IPqIUIuEFzLS0f_ZZ"
      }
    }
  },
  "orgMembership": {
    "-KBFXBBEyvgtfq7h381h": {
      "org": "-KBFXBBEyvgtfqMvU4pi",
      "email": "userA@company.com",
      "permission": "editor"
  }
}

Then I can use the following query:

orgMRef.orderByChild("email").equalTo("userA@company.com").once("child_added", function(snapshot) {
  console.log("found: " + snapshot.key());
});

这篇关于Firebase查询 - 嵌套数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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