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);
Related
database structure
I'm completely new to realtime database and nosql and want some help.
How do I fetch all the data. from only the keys starting with 2020, from the following database structure?
How do I fetch all the data. from only the keys starting with 2020, from the following database structure?
Firebase doesn't provide a way to filter the records by keys that start with a particular String. The most simple solution I can think of is to change the "year" property in your database to be of type number and not String, otherwise, you'll have a lexicographical order. To solve this, you need to use a query that looks like this:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference calendarRef = rootRef.child("calendar");
Query yearQuery = calendarRef.orderByChild("year").startAt(2000).endAt(2001);
yearQuery.addListenerForSingleValueEvent(/* ... */);
This will return all records where the "year" property holds the value of 2000.
I have the following DB structure, then I need get data filtered by "date" field. How do I do that?
root {
places {
$placeUid {
$bookingUid {
date: String
name: String
}
}
}
}
My query looks like that, and it doesn't work:
refPlaces = Firebase.database.getReference("places")
refPlaces.orderByChild("date").equalTo("14.10.2020").addValueEventListener(…)
Firebase queries work on a flat list of child nodes, where the value you order/filter on is in a fixed path under each direct child node. Since you have two dynamic levels under places, you won't be able to query for it.
The solution is to create a flat list of bookings for this use-case, with each booking then having the place Uid as a property in each booking.
For more on this, see:
Firebase Query Double Nested
Firebase query if child of child contains a value
I want to perform a query to a firebase database.
My database is as follows:
I want to query "paymentStatus" by orderByChild value "paid".
How will my query code look? The below code not reading.
Query query = FirebaseDatabase.getInstance().getReference().child("Visits")
.orderByChild("paymentStatus").equalTo("Paid");
Thank you in advance.
Firebase database queries function on a list of child nodes, not on a tree. The database takes each direct child node under the location you query and then sorts/filters on the property you indicate.
There is no property paymentStatus directly under the child nodes of Visits, because the paymentStatus is one level deeper. That means that in your current data model, the query you want is not possible. If you want to filter visits by paymentStatus you will need to ensure that you have a single, flat list of visits.
Also see my answer to Firebase Query Double Nested.
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"
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() { ...