How to sort Firebase records by two fields (Android)
Unfortunately, Firebase Realtime database does not support queries on multiple properties, supports only queries on a single child property. So, you're correct in guessing that you'll need to create an extra field to keep both fields. So to achieve this, you need to create a new field which in your database should look like this:
Firebase-root
|
--- itemId
|
--- a: valueOfA
|
--- b: valueOfB
|
--- a_b: valueOfA_valueOfB
So as you see, the a_b
property combines the values that you want to filter on.
Unlike Firebase Realtime database, Cloud Firestore allows compound queries. You should take a look at this. So a query as the one below is allowed in Cloud Firestore without creating a combined property.
itemIdRef.whereEqualTo("a", "valueOfA").whereEqualTo("b", "valueOfB");
When you order strings this is the normal ordering method. A quick fix would be to add two zeros before each second element like this: "1516428687_001"
, "1516428687_002"
, "1516428687_012"
. Now your order will be fine. For more explanations, please see my answer from this post.
Firebase Query - how to order by a separate field?
The easiest way is to store an extra piece of data in your message
nodes which is the epoch time multiplied by -1
: this way you can sort on this field and the query will return the right order.
See this post on how to get the epoch time: how to get the number of seconds passed since 1970 for a date value?
However, note that with your current data structure, you will encounter a problem: you will need to sort on this new data (e.g. with .orderByChild("messageCreationTime")
) AND filter for a given mChatId (e.g. with equalTo(mChatId)
), which is not possible.
So, you would have to re-structure your database (if this is possible) like this:
- messages
- mChatId
- messageUniqueID <- most probably auto-generated by Firebase
- messageTitle: ....
- messageCreationTime: -1525857044
And you would query by:
Query qChatMessages = databaseReference.child("messages").child(mChatId)
.orderByChild("messageCreationTime");
How to search Firebase database by any field?
The Realtime Database only supports queries on single field property. I have explained in one o my answers:
- How to sort Firebase records by two fields (Android)
How to solve such a situation by creating an extra field to keep the other values. Unfortunately, that workaround doesn't apply to all situations.
Besides that, as far as I know, there is no library that does that automatically in Android. However, there is one for the web, written by David East called Querybase.
So unlike in the Realtime Database, Cloud Firestore allows compound queries. So I recommend you try that.
fetch data from firebase database with multiple where condition in android
Firebase doesn't support multiple orderBy calls. You can apply one filter in server side and another one in client side.
Check below implementation: Here I have used email_id
in server side and date
in client side to filter data. Hope this will help you.
Query query = FirebaseDatabase.getInstance().getReference("Current Location").orderByChild("email_id").equalTo("pratiksiddhapura11@gmail.com");
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
YourModel yourModel = childSnapshot.getValue(YourModel.class);
if(yourModel.getDate().equalsIgnoreCase("2019/11/18")) {
// Here is your desired location
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
How to use multiple filters in android studio with firebase
In realtime database, you cannot use AND
query. Instead you can combine those attributes into one attribute:
Users
randomId
age :20
country: UK
weight : 60
age_country_weight : 20_UK_60
Then you can do:
foodList.orderByChild("age_country_weight").equalTo("20_UK_60")
Related Topics
How to Parse the Androidmanifest.Xml File Inside an .Apk Package
How to Develop a Soft Keyboard For Android
Difference Between Adjustresize and Adjustpan in Android
Android Drawing Separator/Divider Line in Layout
Set Icon For Android Application
Updating Google Play Services in Emulator
Perform a Task on Uninstall in Android
What Does Layoutinflater in Android Do
Disable Scrollview Programmatically
Gradle: Execution Failed for Task ':Processdebugmanifest'
Sending Message Through Whatsapp
Android Youtube App Play Video Intent
How to Use "Share Image Using" Sharing Intent to Share Images in Android
What Android Tools and Methods Work Best to Find Memory/Resource Leaks
Android Deprecated Apache Module (Httpclient, Httpresponse, etc.)
...Have You Declared This Activity in Your Androidmanifest.Xml
How to Grant Modify_Phone_State Permission For Apps Ran on Gingerbread