Is there possible to search database on 6 first digits from the Realtime Database, something like I search for 211010 and the data in range/having the 211010 in front of it will show up like 211010XXXXXXX, 211010XXXXXXX, I want to query data between two numbers (dates).
Asked
Active
Viewed 96 times
0
1 Answers
1
You might be tempted to use a query that looks like this:
DatabaseReference db = FirebaseDatabase.getInstance().getReference();
DatabaseReference pendingRef = db.child("Pending");
Query queryByKey = pendingRef.orderByKey().startAt("211010");
But this will not provide the expected results. Why? Because of the reason provided by @FrankvanPuffelen in the following answer:
So to solve this, the best option that you have is not to use only numerical values for the keys. So a simple solution would be to prefix the keys with a constant String. So the corresponding query should look like this:
Query queryByKey = pendingRef.orderByKey().startAt("*211010").endAt("*211010" + "\uf8ff");
//
And the keys in the database should look like this:
Firebase-root
|
--- *211010...
|
--- *211010...
|
--- *211010...
Edit:
I just took the following example:
{
"Pending" : {
"2110101" : {
"name" : 1
},
"2110102" : {
"name" : 2
},
"2110103" : {
"name" : 3
},
"2110114" : { //It's 21-10-11 and not //It's 21-10-10
"name" : 4
},
}
}
Using the following query (no *
added before the number):
Query queryByKey = pendingRef.orderByKey().startAt("211010").endAt("211010" + "\uf8ff");
I get the following result:
name: 1
name: 2
name: 3
name: 4
So I get all results. However, when I use:
{
"Pending" : {
"*2110101" : {
"name" : 1
},
"*2110102" : {
"name" : 2
},
"*2110103" : {
"name" : 3
},
"*2110114" : { //It's 21-10-11 and not //It's 21-10-10
"name" : 4
},
}
}
And the following query (with the *
added before):
Query queryByKey = pendingRef.orderByKey().startAt("*211010").endAt("*211010" + "\uf8ff");
I only get:
name: 1
name: 2
name: 3
So it works in this case as well.

Alex Mamo
- 130,605
- 17
- 163
- 193
-
so i need to put an simbol infront of data that i would save?, and what if i search for 211010 and for the next 3 days like 211013 – billy Nov 23 '21 at 08:25
-
Yes, that's correct. I choose to use `*`, but you can choose whatever you want, as long as it's not numeric. Give it a try and tell me if it works. – Alex Mamo Nov 23 '21 at 08:28
-
okok letme try it and gonna tell you if it works :) – billy Nov 23 '21 at 08:40
-
Ok, looking forward to seeing your feedback. – Alex Mamo Nov 23 '21 at 08:55
-
Hey @AlexMamo. Great answer as usual --- I doubt that the SDK will coerce to an array in this situation, given the size of the keys. That problem really only occurs when the keys/query results start close to `0`, which seems like it can't happen here. – Frank van Puffelen Nov 23 '21 at 15:17
-
Hey @FrankvanPuffelen Thank you. I did a quick test, as in my updated answer and it works as I originally thought. – Alex Mamo Nov 23 '21 at 15:51
-
@billy Hey. Have you tried my solution above, does it work? – Alex Mamo Nov 24 '21 at 07:26
-
Tbh, im stuck on the date picker design, because the 211013 was a date, stuck on the dateformat rn i need to get a complete 211013 not 13/20/21, i've using (DateFormat.SHORT).format(Calender.getInstance().getTime()) maybe if you know how to, can help me ? – billy Nov 24 '21 at 11:42
-
Try to use [SimpleDateFormat](https://stackoverflow.com/questions/3056703/simpledateformat-ignoring-month-when-parsing) and format it as you like. Give it a try and tell me if it works. – Alex Mamo Nov 24 '21 at 11:45
-
Have solved the issue? Is now everything alright? – Alex Mamo Nov 25 '21 at 07:30
-
i can pick the datepicker alrd, but i got another issue on the date i want to pick, when i try to pick the date 21 10 01 it will be turn into 21 10 1, the zero will affect everything – billy Nov 25 '21 at 17:24
-
That's most likely a format issue. So please add a new question, here on Stackoverflow to get a proper formation. And keep me posted, ok? – Alex Mamo Nov 25 '21 at 20:26