Firebase querying data

2019-01-07 19:30发布

问题:

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

回答1:

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

        }
    });


回答2:

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

    }


});


回答3:

This code works for me

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


回答4:

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