如何使用Firebase查询equalTo(value,key)? [英] How to use Firebase query equalTo(value, key)?

查看:117
本文介绍了如何使用Firebase查询equalTo(value,key)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为firebase中的新手,我试图模仿一种where子句请求来检索用户的钱包,在这个简单的用例中:

  User 
48bde8f8-3b66-40bc-b988-566ccc77335c
电子邮件地址:toto@acme.com
用户名:userTest1

UserWallet
F4PvtvNT2Z
硬币:26
someList
elemet1
elemet2
用户:48bde8f8-3b66-40bc-b988-566ccc77335c

首先,我尝试编写这样的请求:

  Firebase root = new Firebase(https://myApp.firebaseio.com/); 
Firebase ref = root.child(UserWallet);
Query query = ref.equalTo(48bde8f8-3b66-40bc-b988-566ccc77335c,user);

结果为null,所以我写了这个查询:

  Query query = ref.orderByChild(user)。equalTo(48bde8f8-3b66-40bc-b988-566ccc77335c,user); 

结果再次为空。查询钱包的唯一方法是使用以下查询:

pre $查询查询= ref.orderByChild(user)。equalTo 48bde8f8-3b66-40bc-b988-566ccc77335c);

所以我应该总是使用enorderByChild()查询才能使用equalTo() ?
因此,查询equalTo(String value,String key)与equalsTo(String value)比较的目的是什么?因为只有第二个工作正常吗?$ b $有一些边缘情况不需要 orderBy ...(),但是一般来说,在过滤操作( equalTo(),<$)之前,您需要一个 orderBy ...() c $ c> startAt()
endAt())。

建议您先阅读适用于Android的Firebase编程指南(95%适用于普通的Java也是如此),在这个指南中花了几个小时,将会节省数十个问题,例如:这是关于查询的部分。



阅读完这篇文章后,您可能还想阅读<一个href =https:// highlyscala ble.wordpress.com/2012/03/01/nosql-data-modeling-techniques/rel =noreferrer> NoSQL数据建模。它涵盖了NoSQL数据建模中的许多常见模式,并将帮助您尽早实现,试图将SQL查询映射到NoSQL数据库是一个合理的想法,但很少是一个好主意。



UserWallet
48bde8f8-3b66-40bc-b988-566ccc77335c
F4PvtvNT2Z
硬币:26
someList
element1
元素2

在上面的模型中,我倒置了 Wallet User UserWallet 下,这样为用户查找钱包变得更加容易。
$ b $ pre $ ref $($ UserName)。

请注意,这里没有任何查询,因此无论您的数据库中有多少用户。



或者:

$ p $ User
48bde8f8-3b66-40bc-b988-566ccc77335c
邮箱:toto@acme.com
用户名:userTest1

电子钱包
F4PvtvNT2Z
硬币:26
someList
element1
element2

UserWallet
48bde8f8-3b66-40bc -b988-566ccc77335c
F4PvtvNT2Z

现在我们已经完成了扁平化的结构。要确定用户的钱包,请转到 UserWaller / $ uid ,然后从 Wallets / $ walletid 加载每个钱包>。这可能是更多的代码,但它会非常有效(因为没有涉及任何查询)。

As a newbie in firebase I tried to mimic a kind of "where clause" request to retrieve the user's wallet in this simple use case:

User
   48bde8f8-3b66-40bc-b988-566ccc77335c
      email: "toto@acme.com"
      username: "userTest1"

UserWallet
   F4PvtvNT2Z
      coins: 26
      someList
         elemet1
         elemet2 
      user: "48bde8f8-3b66-40bc-b988-566ccc77335c"

First I tried to code my request like this:

Firebase root = new Firebase("https://myApp.firebaseio.com/");
Firebase ref = root.child("UserWallet");
Query query = ref.equalTo("48bde8f8-3b66-40bc-b988-566ccc77335c", "user");

The result was null, So I wrote this query:

Query query = ref.orderByChild("user").equalTo("48bde8f8-3b66-40bc-b988-566ccc77335c", "user");

Result was null again. The only way to retrieve the wallet was with this query:

Query query = ref.orderByChild("user").equalTo("48bde8f8-3b66-40bc-b988-566ccc77335c");

So Should I have to always use en "orderByChild()" query before to use "equalTo()"?
And so, what is the purpose of the query "equalTo(String value, String key)" compare to "equalTo(String value) since only the second one works correctly?

解决方案

There are some edge cases that don't need an orderBy...(), but in general you'll need an orderBy...() before a filtering operation (equalTo(), startAt(), endAt()).

I highly recommend that you first read the Firebase programming guide for Android (95% of it applies to regular Java too). A few hours spent in that guide, will save dozens of questions here. For example: this is the section on queries.

After reading that, you might also want to read this guide on NoSQL Data Modeling. It covers many common patterns in NoSQL data modeling and will help you realize early on that trying to map SQL queries to a NoSQL database is a logical idea, but seldom a good one.

My initial (without any idea on your use-cases, except for "I need to be able to find the wallets for a user") model:

UserWallet
   "48bde8f8-3b66-40bc-b988-566ccc77335c"
      "F4PvtvNT2Z"
         coins: 26
         someList
            element1
            element2 

In the above model, I've inverted the Wallet and User under UserWallet, so that looking up the wallet(s) for a user becomes easier.

ref.child('UserWallet').child(auth.uid).addValueEventListener(...

Note that there is no query involved here, so loading will be equally fast no matter how many users you have in your database.

Or alternatively:

User
   "48bde8f8-3b66-40bc-b988-566ccc77335c"
      email: "toto@acme.com"
      username: "userTest1"

Wallet
   "F4PvtvNT2Z"
      coins: 26
      someList
         element1
         element2 

UserWallet
   "48bde8f8-3b66-40bc-b988-566ccc77335c"
      "F4PvtvNT2Z"

Now we've complete flattened the structure. To determine the wallets of a user, you go to UserWaller/$uid and then load each wallet from Wallets/$walletid. It may be a bit more code, but it'll be extremely efficient (since there are no queries involved).

这篇关于如何使用Firebase查询equalTo(value,key)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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