如何使用Firebase查询equalTo(value,key)? [英] How to use Firebase query equalTo(value, key)?
问题描述
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
在上面的模型中,我倒置了 请注意,这里没有任何查询,因此无论您的数据库中有多少用户。 或者: 现在我们已经完成了扁平化的结构。要确定用户的钱包,请转到 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: First I tried to code my request like this: The result was null, So I wrote this query: Result was null again. The only way to retrieve the wallet was with this query: So Should I have to always use en "orderByChild()" query before to use "equalTo()"? There are some edge cases that don't need an 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: In the above model, I've inverted the 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: Now we've complete flattened the structure. To determine the wallets of a user, you go to 这篇关于如何使用Firebase查询equalTo(value,key)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! 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
加载每个钱包>。这可能是更多的代码,但它会非常有效(因为没有涉及任何查询)。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"
Firebase root = new Firebase("https://myApp.firebaseio.com/");
Firebase ref = root.child("UserWallet");
Query query = ref.equalTo("48bde8f8-3b66-40bc-b988-566ccc77335c", "user");
Query query = ref.orderByChild("user").equalTo("48bde8f8-3b66-40bc-b988-566ccc77335c", "user");
Query query = ref.orderByChild("user").equalTo("48bde8f8-3b66-40bc-b988-566ccc77335c");
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?orderBy...()
, but in general you'll need an orderBy...()
before a filtering operation (equalTo()
, startAt()
, endAt()
).UserWallet
"48bde8f8-3b66-40bc-b988-566ccc77335c"
"F4PvtvNT2Z"
coins: 26
someList
element1
element2
Wallet
and User
under UserWallet
, so that looking up the wallet(s) for a user becomes easier.ref.child('UserWallet').child(auth.uid).addValueEventListener(...
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"
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).