Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Usage of equalTo("value","key") in Firebase?

Here is the following data structure :

{
  "list" : {
    "-K4YlfoWHZqPyWONv68Y" : {
      ".priority" : -1.449077948445E12,
      "date" : 1449077948445,
      "id" : "0",
      "name" : "Name_0"
    },
    "-K4YlfoWHZqPyWONv68Z" : {
      ".priority" : -1.449077948445E12,
      "date" : 1449077948445,
      "id" : "1",
      "name" : "Name_1"
    },
    "-K4YlfoWHZqPyWONv68_" : {
      ".priority" : -1.449077948445E12,
      "date" : 1449077948445,
      "id" : "0",
      "name" : "Name_2"
    },
    "-K4YlfoWHZqPyWONv68a" : {
      ".priority" : -1.449077948445E12,
      "date" : 1449077948445,
      "id" : "1",
      "name" : "Name_3"
    },
 ...
}

I need to only get item where id is "0";

The following is working :

final Firebase ref = new Firebase("https://millezim-test.firebaseio.com/").child("list");

ref.orderByChild("id").equalTo("1").addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot d : dataSnapshot.getChildren()) {
            Model m = d.getValue(Model.class);
        }
    }

    @Override
    public void onCancelled(FirebaseError firebaseError) {

        }
});

But not this one, why ?

ref.equalTo("1","id").addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot d : dataSnapshot.getChildren()) {
            Model m = d.getValue(Model.class);
        }
    }

    @Override
    public void onCancelled(FirebaseError firebaseError) {

        }
});

Did I misunderstand that equalTo("value","key") is to use without reordering By something ?

like image 429
Anthony Avatar asked Dec 02 '15 18:12

Anthony


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.

What is getKey () in Firebase?

Calling the getKey() method on this reference will return the auto-generated key which may then be used to store a corresponding value. The following code, for example, uses the push() method to add a new child at the path stored within the database reference instance: DatabaseReference newChildRef = dbRef.push();

What is limitToFirst in Firebase?

The limitToFirst() method is used to set a maximum number of children to be synced for a given callback. If we set a limit of 100, we will initially only receive up to 100 child_added events. If we have fewer than 100 messages stored in our Database, a child_added event will fire for each message.


1 Answers

The Firebase documentation for equalTo says this about the (optional) key parameter:

The child key to start at, among the children with the previously specified priority. This argument is only allowed if ordering by priority.

Since you're not ordering by priority, you'll need to specify the child with an explicit call to orderByChild().

like image 104
Frank van Puffelen Avatar answered Oct 18 '22 12:10

Frank van Puffelen