18
{
  "random_key 1" : {
    "id": 0,
    "text": "This is text"
  },
  "random_key 2" : {
    "id": 1,
    "text": "This is text"
  }
}

If I'm storing my data like this, and I want to get the node where id is equal to 0. How can I do that?

The above is the child of issue, which is a child of root.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
compiler
  • 486
  • 1
  • 4
  • 14
  • I worded my question a bit differently, but the answer in my post should help you. http://stackoverflow.com/questions/39023945/how-to-get-data-from-real-time-database-in-firebase/39024068#39024068 – David Velasquez Aug 18 '16 at 18:09
  • If you have an existing ID already, why don't you store the data under that ID? It'll make look-ups a lot easier. – Frank van Puffelen Aug 18 '16 at 19:00
  • @FrankvanPuffelen I have stored all the data under ID; which value do I need to pass for orderbychild query on that ID? – Shruti Jun 13 '17 at 09:29

5 Answers5

49

In your case you would have to setup a Query like this:

DatabaseReference reference = FirebaseDatabase.getInstance().getReference();

Query query = reference.child("issue").orderByChild("id").equalTo(0);
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"
            }
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});
Ankit Pise
  • 1,243
  • 11
  • 30
Linxy
  • 2,525
  • 3
  • 22
  • 37
  • Can u please help me on my same type of problem in this question: - http://stackoverflow.com/q/42502024/3946958 ... And can u please tell me that what is the "issue" in your answer???? – Ravindra Kushwaha Mar 01 '17 at 11:32
  • it works for me,, but can you tell how can we filter data with multiple option..like user from country,state,city – Aditya Vyas-Lakhan May 01 '17 at 11:53
  • But what if I dont want to order by orderByChild? I just want to match by id... to me ordering the result is extra computation. – dsharew Jul 21 '17 at 12:49
  • @dsharew You need to orderByChild or orderByKey, or else how would it know that you want to match by "id" and not another child attribute like, in this example, "text". Your other option is make the id the key, then you don't need a query. – Linxy Jul 22 '17 at 19:40
  • @Linxy thanks for the response it seems not possible in firebase but on other database engines it is possible to match without ordering. – dsharew Jul 24 '17 at 06:05
5

@Linxy's answer is correct but since you'll be reading a list of items from the database, it's better to use a child event listener instead of the value event listener.

DatabaseReference reference = FirebaseDatabase.getInstance().getReference();

Query query = reference.child("issue").orderByChild("id").equalTo(0);
query.addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(DataSnapshot dataSnapshot, String s) {
        //Do something with the individual node here`enter code here`
    }

    @Override
    public void onChildChanged(DataSnapshot dataSnapshot, String s) {

    }

    @Override
    public void onChildRemoved(DataSnapshot dataSnapshot) {

    }

    @Override
    public void onChildMoved(DataSnapshot dataSnapshot, String s) {

    }

    @Override
    public void onCancelled(FirebaseError firebaseError) {

    }


});
2

This code works for me

Query query = mFirebaseDatabase.child("issue").orderByChild("id").equalTo(0)
    query.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(final DataSnapshot dataSnapshot) {
                    for (DataSnapshot data : dataSnapshot.getChildren()) {
                     //If email exists then toast shows else store the data on new key
                        if (!data.getValue(User.class).getEmail().equals(email)) {
                            mFirebaseDatabase.child(mFirebaseDatabase.push().getKey()).setValue(new User(name, email));
                        } else {
                            Toast.makeText(ChatListActivity.this, "E-mail already exists.", Toast.LENGTH_SHORT).show();
                        }
                    }
                }
    
                @Override
                public void onCancelled(final DatabaseError databaseError) {
                }
            });
Rahul
  • 3,293
  • 2
  • 31
  • 43
  • 1
    while your solution is still applicable, it's very expensive because it happens on the client-side. Assume you have 1000 records. Fetching all those records (with `onDataChange()`) and looping through all is a pretty expensive task for the client. – Edward Quixote Dec 19 '19 at 15:29
1

How I can use this SQL query in firebase?

SELECT column1, column2, ... FROM table_name WHERE condition1 OR condition2 OR condition3 ...;

0

For an easy-to-use cross platform integration of Firebase you can also have a look at V-Play Engine for mobile apps

FirebaseDatabase {
  id: firebaseDb

  Component.onCompleted: {
    //use query parameter:
    firebaseDb.getValue("public/bigqueryobject", {
                   orderByKey: true,  //order by key before limiting
                   startAt: "c",      //return only keys alphabetically after "c"
                   endAt: "m",        //return only keys alphabetically before "m"
                   limitToFirst: 5,   //return only first 5 sub-keys
    })
  }
}
Joao Vitorino
  • 2,976
  • 3
  • 26
  • 55
user5315753
  • 509
  • 5
  • 3
  • 2
    While it's good you provided an answer, it doesn't really help because it doesn't relate to the question asked. How? Difference in technical environments. The OP asked a question in relation to the Android Development. Your solution is suited for Qt environments. – Edward Quixote Dec 19 '19 at 15:33