I have a node for users with key 1 to 1400 but it's not in order.
Hot to view this in order in the console. I had a similar experience with another node. But it later got ordered automatically
Unfortunately, you cannot change the order of the nodes in the Firebase Database Console. By default, all the nodes are ordered by key. One thing to remember is that Firebase keys are Strings. And when strings are order, are ordered lexicographically.
So for numbers, this is the normal order:
1308
1309
1310
1311
But for strings, this is the normal order:
"1308"
"1309"
"131"
"1310"
There is no operator in Firebase and as far as i know nor in most other databases that allow you to change this behavior. Instead, you will have to modify the data to get the behavior you want. So, store values that are in the order you need them when sorted lexicographically. For numbers you can accomplish that by padding them with zeroes:
"0131" //zero added before
"0132" //zero added before
......
"1308"
"1309"
"1310"
"1311"
Related
I'm saving list of items (by adding item one by one) from my app to firebase database, when I add 10th element it got saved after 1 (key) node not below 9th one and 2nd got shifted below 10 as you can see in the image below . And list of items are shown in the RecyclerView in the same sequence as it is saved in firebase databse . Why are the items saved in such a manner??
This is the expected behaviors. Keys in the Firebase Realtime Database are strings, and they are shown in so-called lexicographical order.
If you'd use letters, you'd also expect this order:
a
b
ba
c
And it is no different for numbers when they are sorted lexicographically, as they are here.
If you want to see a numeric sort order, consider padding the numbers to a fixed with, for example by prefixing them with zeroes:
crop: 000
crop: 001
crop: 002
...
crop: 010
Note that in general using such numerical sequential values as keys is an anti-pattern in Firebase, and it's recommended to use its built-in push keys instead. For more on this see: Best Practices: Arrays in Firebase.
I am trying to make blocks of data (10 each) as query.
The limitToLast() and limitToFirst() work without each other but crashes the app when together.
query = db_reference_type.limitToLast((int) (long_total_buildings - (long_list_page * 10))).limitToFirst(10);
With long_list_page = 0 and long_total_buildings = 10
query = db_reference_type.limitToLast(10).limitToFirst(10);
The app just crashes when the program gets to that line.
There is no way to combine limitToFirst() and limitToLast() in a single query. You seem to be building a pagination system, and looking for an offset() condition, which doesn't exist in Firebase.
It's easier to understand what is possible once you know how Firebase processes your request.
When it receives a query at a location, Firebase retrieves an index of the items on that location on the property that you order the query on (or on the key if no order is specified).
It then finds the value that you've indicated you want to start returning items from. So this is a value of the property that you order the query on (or on the key if no order is specified).
It then finds the value that you've indicated you want to end returning items at. This too is a value of the property that you order the query on (or on the key if no order is specified).
So at this point the database has a range of values in the index, and by association the keys of the items matching those values.
It then clips the items in the range by either the number of items from the start (if you use limitToFirst()) or from the end (if you use limitToLast).
And then finally it returns the remaining items that match all criteria.
Note that the value you pass to limitToFirst/limitToLast is only used in the last step of this process. It is not used in step 3, which is what you seem to be trying.
This question already has an answer here:
Firebase query if child of child contains a value
(1 answer)
Closed 3 years ago.
I need a firebase query to filter the list based on the value of an array.
if any of the index of GID(Array) contains the given key. e.g my key is YsGMyfLSGRNHxDWQmhpuPRqtxlq1 and one node's GID have that on 0th index and other have that on 1st index. So these two lists need to be returned.
Currently, I can only get the one at 0th index using the code
//userID = YsGMyfLSGRNHxDWQmhpuPRqtxlq1
firebaseDatabase.child("Groups").queryOrdered(byChild: "GID/0").queryEqual(toValue:userID)
When I try to combine the query I am getting errors.
I don't know about your database structure, But I can explain that There is a limitation in Firebase Realtime database that you can only order by 1 child.
So now if we require to order by 2 Childs we can combine 2 nodes and make it 1 node and can apply order by query on it. For example
If we have username & email fields we can make a new field username_email and can apply order by on it.
Like
user: {
username: "john",
email: "john#g.com"
username_email = "john_john#g.com"
}
Now we can write
firebaseDatabase.child("user").queryOrdered(byChild: "username_email").queryEqual(toValue: "john_john#g.com");
There is no way you can filter your groups based on a value that exist within an array. If you want to query your database to get all groups a particular user is apart of, then you should consider augmenting your data structure to allow a reverse lookup. This means that you should add under each user object the groups in which that user is present.
This means that you'll need to duplicate some data, but this is not a problem when it comes to Firebase. This is a quite common practice, which is named denormalization and for that, I recommend you see this video, Denormalization is normal with the Firebase Database.
When you are duplicating data, there is one thing that need to keep in mind. In the same way you are adding data, you need to maintain it. With other words, if you want to update/detele an item, you need to do it in every place that it exists.
However, what you need is actually allowed in Cloud Firestore. Its array-contains operator allow you to filter documents that have a certain value in an array. For more on this topic, please see the following post:
Better Arrays in Cloud Firestore.
I want to get sorted data as.... value1--value2--value3....value10--value11
String values are sorted lexicographically. If you want them to be sorted in the order you want, you'll need to ensure that the lexicographical order matches your needs, e.g. pad the numbers in your keys to all be the same length:
"value01": ...
..
"value09": ...
"value10": ...
I wrote a longer answer on this a while ago, see: Firebase query ordering not working properly
I would like to perform wildcard queries on a Firebase database,
and do not know whether the libraries support this. - (my guess it does not.)
I want an optimal solution where the network traffic will be as little as possible, assume the dataset is millions of records.
Dataset (using prefix wildcard):
user_id:
id_001123:
name: "java"
id_002124:
name: "objective-c"
id_003125:
name: "swift"
How would the code look like to retrieve the record id_002124 name field using a wildcard, you only have a portion of the id. eg. "%124".
The expected result would be id_002124, and the name being "objective-c"
Any mobile language example code would great.
(Objective-C/Swift/Java)
Firebase Database queries can order/filter data based on the object's keys (e.g. id_001123), their values (doesn't apply in your case), or the value of a child property (e.g. the fact that name = "java").
For each of these Firebase can match items that have a specific value you specify, items starting at a value you specify, or items ending at a value you specify.
So you can create a query matching the item named objective-c with:
ref.child("user_id").orderByChild("name").equalTo("objective-c")
Or you can create a query matching id_001124 and id_001125 with:
ref.child("user_id").orderByKey().startAt("id_001125")
Or simply getting all items starting with id_0011:
ref.child("user_id").orderByKey().startAt("id_0011")
Or you can create a query matching id_001123 and id_001124 with:
ref.child("user_id").orderByKey().endAt("id_001124")
Firebase Database cannot filter based on the end of a value. So there is no way in your current data structure to get all items whose key ends in a 4.
Please read more about Firebase Database queries here: https://firebase.google.com/docs/database/android/lists-of-data#sorting_and_filtering_data
And see some of the many previous questions about searching for FirebaseL
How to perform sql "LIKE" operation on firebase?
Firebase query - Find item with child that contains string
Firebase "like" search on string