Simple Android / Firebase orderByChild / equalTo - android

This I suspect is easy, but I can't get it to work as I want. I'm referencing a database ref with my query information for my Firebase database. The code below works fine, but I can't hard code in Match_01 (this was purely done to get the code working).
String getArgument = getArguments().getString("matchid");
final DatabaseReference ref=FirebaseDatabase.getInstance().getReference().child("Matches").child("Match_01");
What I need to do is use the matchID thats been passed to the fragment and use equalTo instead of referencing the final child node.
final DatabaseReference ref=FirebaseDatabase.getInstance().getReference().child("Matches").orderByChild("gameID").equalTo(getArgument);
But this doesn't work, I can't swap out the last child reference for the orderByChild reference.
All help is appreciated.

I'm assuming from your question that the Matches node's children have the id you want to reference dynamically as a key.
Then, you need orderByKey instead of orderByChild. This should work:
final DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Matches");
Query q=ref.orderByKey().equalTo(getArgument);
When using orderByChild, the query will match nodes against this node's attributes. In other words, your attempt would work on a collection with the following structure:
- Matches
- $key
- gameID: "Match_01"

Related

Not able to use getRef() of Firebase in android

I need to update the data in Firebase database. I have displayed the data in Recyclerview. I need to get the child reference of the position i click in Recyclerview. An not able to use getRef() there to get the reference.
You need to generate a unique key with
String unique = reference.push().getKey();
when saving data to firebase, then you can use that unique key to modify content of that node later.
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Transactions").child(uniqueId)...
You can show your getRef(position) method in your question, maybe another solution can come from there.

For android firebase how to write the query for finding child by string matching of child of child's value?

Assume this script:
private static DatabaseReference;
mUserDatabase = FirebaseDatabase.getInstance().getReference("Users");
Query firebaseSearchQuery = mUserDatabase.child("service").orderByChild("serviceName").startAt(searchText).endAt(searchText+"\uf8ff");
FirebaseRecyclerAdapter<Workers, WorkerViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Workers, WorkerViewHolder>(
Workers.class,
R.layout.search_result_layout,
WorkerViewHolder.class,
firebaseSearchQuery
) {
#Override
protected void populateViewHolder(WorkerViewHolder viewHolder, Workers model, int position) {
viewHolder.setDetails(model.getName(), model.getEmail());
}
};
What will be the query for finding users email by searching child of child's value (hardware) ?
There is no way to achieve this using this database structure.
First of all between Users node and service node there is another node, named userId, which does not appear in any reference. Between the service node and the child serviceName there is another node, serviceId node, which is the unique key generated by the push() method. I also don't see where are you looping to get those childrens. But these are not the actual problems.
In such cases as yours, you need to use a tehnique named in Firebase, denormalization and for that I recomend you see this tutorial, Denormalization is normal with the Firebase Database, for a better understanding.
So in your case, you need to create another node that will host only the services, so you can simply create a query. Unfortunately, Firebase does not allow you to query after multiple fields, so this is your only choice.

Query nested data from Firebase Real-time database Android

I have the structure in the image below and I want to get the whole data from the node "aaa1234" querying by "id_Usuario". How can I do that query?
I tried:
DatabaseReference root = database.getReference().child("Eventos").child("participantes");
Query query = root.orderByChild("id_Usuario").equalTo(2);
Just to be more clear, the "aaa1234" is a custom key, so it will be different in the next node.
I see two mistakes.
The first one is a typo, which I already marked in my comment. You're storing the user ID as a number, so shouldn't have quotes around the value in equalTo. So: .equalTo(2)
The second mistake is in the way you try to query:
DatabaseReference root = database.getReference().child("Eventos").child("participantes");
This will create a reference to the non-existing child Eventos/participantes. What you want to do instead is query Eventos, but then against property participantes/id_Usuario. So
DatabaseReference root = database.getReference().child("Eventos");
Query query = root.orderByChild("participantes/id_Usuario").equalTo(2);

how to filter firebase object using field inside list

I am an android developer, but this is not really important. I accept solution also in other programming language.
I have a collection of "answers" under /root/answers. Here the data is stored using push method, so each child has a unique id. Each child has a field called "user_ref" which is a simple string. I need to populate my list with all answers where a given string match with "user_ref".
So I built my DatabaseReference with:
DatabaseReference r = FirebaseDatabase.getInstance().getReference(ANSWERS_REF)
.startAt("id_123")
.endAt("id_123")
.getRef();
It works but every time this Database references return all answers and not only selected
Try to use equalTo("id_123") instead of startAt() and endAt() (I am assuming that id_123 is a value for user_ref). Namely as follows,
DatabaseReference r = FirebaseDatabase.getInstance().getReference(ANSWERS_REF)
.orderByChild("user_ref")
.equalTo("id_123")
.getRef();
Hope it helps!
The problem is caused by calling getRef() at the end of your statement. That returns the DatabaseReference of the location that you query, so that is all answers.
DatabaseReference allAnswers = FirebaseDatabase.getInstance().getReference(ANSWERS_REF);
Query r = allAnswers.startAt("id_123")
.endAt("id_123");
Since Query is the base class of DatabaseReference, you can also attach listeners to the Query.
r.addChildEventListener(new ChildEventListener() { ...

Android firebase query equivalence

In my Android app I need to compare equivalence of two Firebase database query objects. Is there some way for me to assert that two query objects are equivalent based on their database references and query parameters?
The Javascript Firebase version of the Query class has an isEqual() method which produces the desired behaviour.
https://firebase.google.com/docs/reference/js/firebase.database.Query
I tried the following equivalence test in my Android code but this does not produce expected behaviour:
Reference ref = Firebase.getInstance();
String child = "childKey"; //this is a valid child of ref
Query query1 = ref.child(child).limitTo(10);
Query query2 = ref.child(child).limitTo(10);
query1 == query2 //false
query1.equals(query2) //false
This seems to work
query1.zzWM().toString.equals(query2.zzWM().toString) // true
I don't think you'd need this though. For as far as I know Firebase keeps the results cached for as long as there is a listener on a reference. So even if you would have 2 it would just get it from the cached version, but I might be wrong.

Categories

Resources