Android Firebase Database query select with condition - android

I want to SELECT * FROM products WHERE 'birthday' = true;
But can I do the same with Firebase Database ?
I have tried
DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
ref. child("products").orderByChild("birthday").equalTo(true).addChildEventListener ........
Firebase Database image
But it doesn't work.Please help.
f my data structure is bad, please also advise how to create a better structure.

The screenshot of the database shows that birthday is not a direct child of the product node: there's an extra step, for_purpose.
Your query must follow the schema precisely, therefore update your query code like this:
DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
ref.child("products")
.orderByChild("for_purpose/birthday")
.equalTo(true)
.addChildEventListener ...
Note that for this to work effectively, you must set up indexes for every possible for_purpose child in your security rules. Or at least for those that you use in queries. The indexes must match your query exactly, putting an index on for_purpose is not enough.
For this question, you need at least the following:
{
"rules": {
"products": {
".indexOn": [
"for_purpose/birthday",
"for_purpose/anniversary",
"for_gender/male",
...
]
}
}
}

Related

How to get all posts where current user has not commented yet using firebase realtime database?

My use case is to fetch all posts which is not already commented by current user.
I want to achieve this with single query but my current approach is not working.
DB structure:
posts: {
post1:{
content: "Lorem Ipsum"
comments: {
uid1: true,
uid2: true
}
}
}
My query looks like this:
val postsRef = FirebaseDatabase.getInstance().reference.child("posts")
.orderByChild("comments/{uid}")
.equalTo(null)
Above query an index, but my current index is one dynamic value which firebase doesn't allow.
"posts" : {
"$postId": {
"comments": {
".indexOn" : ".value"
}
}
}
I'm stuck here, it would be helpful if anyone guide me to achieve this.
Thanks
Firebase database queries can only check for the presence of a certain value, not for the absence of it.
So you can either add each user ID to each post and then remove them when they comment, or (more likely) you will have to implement the logic in your application without a database query: just load all posts and remove the ones where you then find that this user already commented.

How to get retrieve items from the following realtime database structure?

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.

Can't sort data from firebase realtime database by timeStamp

I am making a challenges between users in my app .I am trying to get the last 15 users who enters in challenges. I store the time each time the users enter a new challenge. The query was working well in the begging but after that it stops showing new users and only old users appears in it.
this is the query code :
usersReference.orderByChild("lastChallengeDate")
.limitToLast(15)
.addListenerForSingleValueEvent(new ValueEventListener()
and this is the database structre of the user child :
When I opened the log I found this warn although I am using index on in my rules
W/PersistentConnection: pc_0 - Using an unspecified index. Consider adding '".indexOn": "lastChallengeDate"' at users to your security and Firebase Database rules for better performance
If you perform a query on a location, Firebase sorts the children under that location on the property you specify. There is no value in lastChallengeDate directly under each child of users. Instead the property is under lastChallengeDate/time, so you should order on that
usersReference.orderByChild("lastChallengeDate/time")
.limitToLast(15)
.addListenerForSingleValueEvent(new ValueEventListener()
You also need to define an index on users (or whatever the name is of the node you query):
{
"rules": {
"users": {
".indexOn": "lastChallengeDate/time"
}
}
}
Be sure to also study:
the documentation on queries, which includes an example of querying such a nested property
the documentation on defining indexes
some of the many questions with the same error message

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

Querying on Firebase Database with large data set is very very slow

I use Firebase database on my Android app. Normally, it works fine. But when the database is getting larger, the query performance is getting worse. I added about 5k record on database (under "elk" and "su" nodes), then I queried on database (on "cut" and "user" nodes) but all the queries are very very slow. I defined data index on database rules but it did not work. How can I solve that problem?
Here are my queries :
// query to get the zones followed by user
FirebaseDatabase.getInstance()
.getReference()
.child("user")
.child(userID)
.child("zones");
// query to get cuts on a zone
FirebaseDatabase.getInstance()
.getReference()
.child("cut")
.child(cutType)
.orderByChild("zoneID")
.equalTo(zoneID);
If you want to continue expanding the best thing to do would be to duplicate your data in a zone reference where it knows which elk/su are a part of it. Something like this:
{
zones: {
elk: {
"istan-besik": {
"-KSp)bL5....": true,
...: true
}
}
}
}
That way when you want to search for all you would just do:
...child('zones').child(zoneId).child(cutType)
And then loop through those to go get each elk/su directly

Categories

Resources