Java 如何使用 Firebase 查询 equalTo(value, key)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35552571/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 16:49:09  来源:igfitidea点击:

How to use Firebase query equalTo(value, key)?

javaandroidfirebasefirebase-realtime-database

提问by ThierryC

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:

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

User
   48bde8f8-3b66-40bc-b988-566ccc77335c
      email: "[email protected]"
      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?

那么在使用“equalTo()”之前,我是否必须始终使用“orderByChild()”查询?
因此,查询“equalTo(String value, String key)”与“equalTo(String value)”相比的目的是什么,因为只有第二个可以正常工作?

回答by Frank van Puffelen

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()).

有一些边缘情况不需要orderBy...(),但通常orderBy...()在过滤操作(equalTo(), startAt(), endAt())之前需要 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.

我强烈建议您首先阅读适用于 AndroidFirebase 编程指南(其中 95% 也适用于常规 Java)。在该指南中花费几个小时,将在这里节省数十个问题。例如:这是关于查询部分

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.

读完之后,您可能还想阅读有关NoSQL 数据建模的本指南。它涵盖了 NoSQL 数据建模中的许多常见模式,并将帮助您尽早意识到尝试将 SQL 查询映射到 NoSQL 数据库是一个合乎逻辑的想法,但很少是好的想法。

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 Walletand Userunder UserWallet, so that looking up the wallet(s) for a user becomes easier.

在上述模型中,我已经倒了Wallet,并UserUserWallet,这样对于用户查找钱包(S)变得更容易。

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: "[email protected]"
      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/$uidand 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).

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

回答by Atif AbbAsi

You can use nested Query for this.! if you have multiple random id you can easily compare them.! DatabaseReference reference = FirebaseDatabase.getInstance().getReference();

您可以为此使用嵌套查询。!如果您有多个随机 ID,则可以轻松比较它们。!DatabaseReference 参考 = FirebaseDatabase.getInstance().getReference();

    Query query = reference.child("user");
    query.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if (dataSnapshot.exists()) {
                // dataSnapshot is the "issue" node with all children with id 0
                for (DataSnapshot issue : dataSnapshot.getChildren()) {
                    // do something with the individual "issues"

Query query = reference.child("user").child(dataSnapshot.getKey().equals(YourData)));
    query.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if (dataSnapshot.exists()) {






                }

            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });