compiler
compiler

Reputation: 496

Firebase querying data

{
  "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.

Upvotes: 18

Views: 51344

Answers (5)

Muhammad Arslan Arain
Muhammad Arslan Arain

Reputation: 21

How I can use this SQL query in firebase?

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

Upvotes: 1

Rahul
Rahul

Reputation: 3349

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) {
                }
            });

Upvotes: 2

Linxy
Linxy

Reputation: 2635

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) {

    }
});

Upvotes: 49

user5315753
user5315753

Reputation: 539

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
    })
  }
}

Upvotes: 0

@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) {

    }


});

Upvotes: 5

Related Questions