在firebase中查询嵌套的数据 [英] querying nested data in firebase

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

问题描述

现在新的查询功能已经不存在了,我会问这个问题。这与其他问题类似,答案是基于旧API。

示例数据:


accounts:{
simplelogin:001:{
email:test1@test.com,
移动:+15555551001,
...更多字段
},
simplelogin:002:{
email:test2@test.com ,
mobile:+15555551001,
...更多字段
},
simplelogin:003:{
email: test3@test.com,
mobile:+15555551003,
...更多字段
}
}



我希望新的查询capabilites将允许查询嵌套的数据。但是,根据我所看到的,为了找到电子邮件地址为test2@test.com的帐户,我真的必须做两件事之一:


  1. 为accounts集合执行 .on ,然后自己过滤结果

  2. 创建一个具有如下记录的参考表,然后在参考表上查询以获取到帐户表的链接。




$ account
$ account $ b $ email $ $ $ $ $ $ $ $ $ $ $ test.com,
mobile:+15555551001
},
{
account:simplelogin:002,
email: test2@test.com,
mobile:+15555551001
},
{
account:simplelogin:003,
电子邮件:test3@test.com,
mobile:+15555551003
}
]



这些是我唯一的选择吗?是不是可以使用新的查询API来更深入地查询嵌套信息?

解决方案

如果你的问题是我怎么能根据它的电子邮件获得一个账户?,然后:

  var ref = new Firebase(https://your.firebaseio的.com /帐户); ('value',function(snapshot){
console.log(snapshot.val);

ref.orderByChild('email')。equalTo('test2@test.com')。 ());
});

请参阅jsbin以获取工作示例: http://jsbin.com/yikecu/1/edit?js,console



< h2> Update

事实证明,OP为每个帐户都有一个嵌套的电子邮件地址集合,并希望根据一个电子邮件地址的存在来选择项目。

  var data = {
accounts:{
simplelogin:001:{
emails:[test1@test.com,'test1@bla.com'],
mobile:+15555551001
},
simplelogin:002 :{
emails:[test2@test.com,'test2@bla.com'],
mobile:+15555551001
},
simplelogin:003:{
emails:[test3@test.com,'test3@bla.com'],
mobile:+15555551003
}
}
};

我确实不认为目前可以使用包含查询。因此,自己创建索引是最好的方法。我会保持该索引非常简单:

  email_to_uid:
test1@test.com:simplelogin:0001
test1@bla.com:simplelogin:0001
test2@test.com:simplelogin:0002
test3@bla.com:simplelogin:0002
test3@test.com:simplelogin:0003
test3@bla.com:simplelogin:0003

此索引假定每个电子邮件地址映射到至多一个帐户。如果这不符合您的使用情况,则需要相应地修改数据结构。


Now that the new query capabilities are out, I'm going to ask this question. It's similar to other questions where the answers were based on the old API.

Example Data :

{ "accounts" : { "simplelogin:001" : { "email" : "test1@test.com", "mobile" : "+15555551001", ... many more fields }, "simplelogin:002" : { "email" : "test2@test.com", "mobile" : "+15555551001", ... many more fields }, "simplelogin:003" : { "email" : "test3@test.com", "mobile" : "+15555551003", ... many more fields } }

I was hoping the new query capabilites would allow querying nested data. However, based on what I see, in order to find the account with the email address "test2@test.com", I really have to do one of two things:

  1. Do an .on for the accounts collection and then filter the results myself.
  2. Create a reference table that has records like below and then query on the reference table to get the link to the accounts table.

[ { "account": "simplelogin:001", "email": "test1@test.com", "mobile": "+15555551001" }, { "account": "simplelogin:002", "email": "test2@test.com", "mobile": "+15555551001" }, { "account": "simplelogin:003", "email": "test3@test.com", "mobile": "+15555551003" } ]

Are these really my only options? Is it not possible to use the new query API to query deeper into nested info?

解决方案

If your question is "how can I get an account based on its email?", then:

var ref = new Firebase("https://your.firebaseio.com/accounts");

ref.orderByChild('email').equalTo('test2@test.com').on('value', function(snapshot) {
  console.log(snapshot.val());
});

See this jsbin for a working sample: http://jsbin.com/yikecu/1/edit?js,console

Update

It turns out OP has a nested collection of email addresses for each account and want to select items based on the presence of one email addresses.

var data = {
  "accounts" : {
    "simplelogin:001" : {
      "emails" : ["test1@test.com", 'test1@bla.com'],
      "mobile" : "+15555551001"
    },
    "simplelogin:002" : {
      "emails" : ["test2@test.com", 'test2@bla.com'],
      "mobile" : "+15555551001"
    },
    "simplelogin:003" : {
      "emails" : ["test3@test.com", 'test3@bla.com'],
      "mobile" : "+15555551003"
    }
  }
};

I indeed don't think that a "contains" query is currently possible. So creating an index yourself is then the best approach. I would keep that index extremely simple:

email_to_uid:
    test1@test.com: simplelogin:0001
    test1@bla.com: simplelogin:0001
    test2@test.com: simplelogin:0002
    test3@bla.com: simplelogin:0002
    test3@test.com: simplelogin:0003
    test3@bla.com: simplelogin:0003

This index assumes that each email address maps to at most one account. If that does not fit your use-case, you'll need to modify the data structure accordingly.

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

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