Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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: "[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?

like image 226
ThierryC Avatar asked Feb 22 '16 11:02

ThierryC


People also ask

What is key and value in Firebase?

In Firebase Database everything is a node, that follows the pattern key: value. Firebase Database provides us with a simple way to generate unique keys. Unique keys create new items while uploading data to a previously stored key will update.

Is Firebase a key value database?

Firebase uses a very popular style of NoSQL database: Key-value stores. The concept is not so complicated once you know the basic JSON syntax.

How do I get a key in Firebase?

Firebase automatically creates API keys for your project when you do any of the following: Create a Firebase project > Browser key auto-created. Create a Firebase Apple App > iOS key auto-created. Create a Firebase Android App > Android key auto-created.

Which method is used to fetch any key from Firebase?

We can use HashMap.


1 Answers

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: "[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/$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).

like image 164
Frank van Puffelen Avatar answered Sep 23 '22 19:09

Frank van Puffelen