Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 844
compiler Avatar asked Aug 18 '16 17:08

compiler


People also ask

Can you query from Firebase?

Querying DataWith Firebase database queries, you can selectively retrieve data based on various factors. To construct a query in your database, you start by specifying how you want your data to be ordered using one of the ordering functions: orderByChild() , orderByKey() , or orderByValue() .

How do I get particular data from Firebase?

Firebase data is retrieved by either a one time call to GetValueAsync() or attaching to an event on a FirebaseDatabase reference. The event listener is called once for the initial state of the data and again anytime the data changes.

What query language does Firebase use?

Firebase and MySQL Differences Firebase uses NoSQL; MySQL uses SQL.

Is Firebase good for scaling?

Automatic Scaling As and when there is a change in data, Firebase helps in the calculation of the minimum set of updates needed to keep all your clients synchronized. Additionally, the API functions of firebase are designed in order to scale linearly with the size of data being synchronized.


1 Answers

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

    }
});
like image 134
Linxy Avatar answered Nov 16 '22 01:11

Linxy