在firebase中查询嵌套的数据

编程入门 行业动态 更新时间:2024-10-18 14:22:16
本文介绍了在firebase中查询嵌套的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 现在新的查询功能已经不存在了,我会问这个问题。这与其他问题类似,答案是基于旧API。

示例数据:

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

  • 为accounts集合执行 .on ,然后自己过滤结果。
  • 创建一个具有如下记录的参考表,然后在参考表上查询以获取到帐户表的链接。
  • $ account $ account $ b $ email $ $ $ $ $ $ $ $ $ $ $ test,mobile:+15555551001}, {account:simplelogin:002,email: test2@test,mobile:+15555551001}, {account:simplelogin:003,电子邮件:test3@test,mobile:+15555551003} ]

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

    解决方案

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

    var ref = new Firebase(https://your.firebaseio的 /帐户); ('value',function(snapshot){ console.log(snapshot.val); ref.orderByChild('email')。equalTo('test2@test')。 ()); });

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

    Update

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

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

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

    email_to_uid: test1@test:simplelogin:0001 test1@bla:simplelogin:0001 test2@test:simplelogin:0002 test3@bla:simplelogin:0002 test3@test:simplelogin:0003 test3@bla: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", "mobile" : "+15555551001", ... many more fields }, "simplelogin:002" : { "email" : "test2@test", "mobile" : "+15555551001", ... many more fields }, "simplelogin:003" : { "email" : "test3@test", "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", I really have to do one of two things:

  • Do an .on for the accounts collection and then filter the results myself.
  • 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", "mobile": "+15555551001" }, { "account": "simplelogin:002", "email": "test2@test", "mobile": "+15555551001" }, { "account": "simplelogin:003", "email": "test3@test", "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("your.firebaseio/accounts"); ref.orderByChild('email').equalTo('test2@test').on('value', function(snapshot) { console.log(snapshot.val()); });

    See this jsbin for a working sample: jsbin/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", 'test1@bla'], "mobile" : "+15555551001" }, "simplelogin:002" : { "emails" : ["test2@test", 'test2@bla'], "mobile" : "+15555551001" }, "simplelogin:003" : { "emails" : ["test3@test", 'test3@bla'], "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: simplelogin:0001 test1@bla: simplelogin:0001 test2@test: simplelogin:0002 test3@bla: simplelogin:0002 test3@test: simplelogin:0003 test3@bla: 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中查询嵌套的数据

    本文发布于:2023-10-23 05:37:24,感谢您对本站的认可!
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:嵌套   数据   firebase

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!