Query by key and order by date
You may want to review the Complex Queries in Firebase documentation.
Take a look at range queries. startAt()
and endAt()
allow you to look for shoes with specific sizes by setting starting and ending points for your queries.
For example, if we wanted to find all shoes that are between 9
and 10.5
, combine orderByChild()
, startAt
, and endAt
. Something like:
var ref = new Firebase(URL);
ref.orderByChild("timestamp").startAt("9").endAt("10.5").on("child_added", function(snapshot) {
console.log(snapshot.key());
});
If you could provide a code sample, then it would make it easier to pinpoint the solution. You may have to tweak your data model if you want to order your query by multiple keys.
Search by key, order by value
You can't order the same ref multiple times as documented here
When you use a order
or a filter
method, it returns a Query Interface. See it as a filtered reference containing only a subset of the original data. It means that
databaseRef.child("userFavorites").orderByKey().equalTo(user.uid)
will not return userFavorite/${user.uid}
but userFavorite
filtered to show only the user.uid
entry. You can see it by doing
databaseRef.child("userFavorites").orderByKey().equalTo(user.uid).ref.key
That should return 'userFavorites'
In your case, I see two options:
Keep going with orderByKey().equalTo()
and sort the results yourself
Or use directly child()
to get the user, then sort via Firebase (and don't forget to use the Firebase's snapshot.forEach
to be sure you get the data in the query order):
databaseRef.child(`userFavorites/${user.uid}`).orderByValue().once('value', (snapshot) => {
if (snapshot.exists()) {
snapshot.forEach((child) => {
console.log(`${child.key}: ${child.val()}`)
})
}
})
Firebase database ordering by keys number
You were right to remove the prefixed 0. It's because Firebase Database keys are strings ordered lexicographically, and therefore it was returning them in an 'unnatural' looking order.
JavaScript - how to order firebase data by key of childs
to order by position
, try the following:
db_ref.orderByChild("position").once('value', function(snapshot){
from the docs:
https://firebase.google.com/docs/reference/js/firebase.database.Reference#orderByChild
orderByChild
Generates a new Query object ordered by the specified child key.
Queries can only order by one key at a time. Calling
orderByChild()
multiple times on the same query is an error.
How to order the nodes in firebase console based on key
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"
Sort Keys in Firebase Database Android
Storing the rating
as a String
is not the the best option when it comes to sorting data in a Firebase database. If you keep the rating as a String, remember that items are ordered lexicographically
. Let's take an example. If you have used 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. So for that, I strongly recommend you change the data type to Integer
or Long
.
If you already have released the app, but I doubt it, because I see some testing values in your database, you need to know that is something that you can do, even if you are using strings. You can modify the data to get the behavior you want. So, you can store the values that are in the order you need when are sorted lexicographically. You can accomplish that by padding them with zeroes:
- "0131" //zero added before
- "0132" //zero added before
- ......
- "1308"
- "1309"
- "1310"
- "1311"
Assuming you have changed the type of the rating
field and you want to get a user property, to sort the item by rating, please use the following code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference carpentersRef = rootRef.child("AvailableWorkers").child("Carpenters");
Query query = carpentersRef.orderByChild("rating")
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String g = ds.child("g").getValue(String.class);
Log.d("TAG", g);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {}
};
query.addListenerForSingleValueEvent(valueEventListener);
The output will be the values of g
property ordered by rating
property.
How to sort Firebase Keys in ascending order?
Firebase keys are strings. And when you order string data, it is ordered lexicographically.
So for numbers, this is the normal order:
- 1
- 9
- 10
- 11
But for strings, this is the normal order:
- "1"
- "11"
- "19"
- "9"
There is no operator in Firebase (nor in most other databases) 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:
- "001"
- "009"
- "011"
- "019"
Or in your case c09p010
, etc.
Related Topics
Saving Nsimage in Different Formats Locally
How Set Custom Fonts for iOS13 Context Menu Actions
How to Create a Circle with Rounded Ends for Each Quadrant
How to Stop a Dispatchqueue in Swift
Making a Uibutton a % of the Screen Size
How to Delete Object in Array of Dictionaries Using Key Value
How to Make Prepareforsegue with Uicollectionview
Change Width of a Uibarbuttonitem in a Uinavigationbar in Swift
Swift Codable with Different Array Types
Swift Alamofire Add Custom Header to All Requests
How to Add Openssl to a Swift Project
Changing the Color of the Icons in a Uitextfield Inside a Uisearchbar
Swift. Is the (Absolutely) Sole Specific Advantage of Unowned Over Weak, Performance
Which Value Types in Swift Supports Copy-On-Write
Connecting Hc-05 with iPhone Se iOS(V11.0)
Swift 4.2, String Firstindex() Function Error in Xcode Playground