This question already has answers here:
Query based on multiple where clauses in Firebase
(8 answers)
Closed 5 years ago.
Query q = FirebaseDatabase.getInstance().getReference().child("Chats").orderByChild("senderkey")
.equalTo(FirebaseAuth.getInstance().getCurrentUser().getUid());
In the above code I'm querying using sender = x , what if i want to make this statement senderkey = x and receiverkey = y.
In short, how to use AND in firebase ?
because:
I’m displaying the data by querying by senderkey but I don’t want to display the duplicated data again, if the senderkey is repeated i want it to be displayed once
I don't think this is possible. You can't query on multiple children. If you know you want the last message with a specific sender id you can use the limitToLast() function which will eliminate the duplicate concern.
More info on Firebase Database Queries here: https://firebase.google.com/docs/database/admin/retrieve-data
These type of queries are not allowed in firebase realtime database You can use firestore for multiple where queries by the way.
If you want to stick with firebase realtime database then you should work on your data model and create combine key to search and perform AND operation.
For example to perform AND operation you need to create a key
"name_createdAt" = "chickungunya_moka"
and then do query for
orderByChild("name_reportedAt").equalsTo(name+"_"+reportedAt);
Related
This question already has answers here:
Android Firebase multiple sort
(1 answer)
How to sort Firebase records by two fields (Android) [duplicate]
(1 answer)
Filter and sort on multiple values with Firebase
(1 answer)
Closed 3 years ago.
I am using Firebase Recycler Adapter on the Real Time Data Base (RTDB). Unfortunately, the query feature for the RTDB is quite limited and is causing me some problems. Here is the problem I am trying to solve:
Imagine I have an object model consisting of the name and the national origin of a person. I can query all the people with a particular national origin as shown below:
Query query = FirebaseDatabase.getInstance().getReference().child("People").orderByChild("nationalOrigin").equalTo("USA");
FirebaseRecyclerOptions<People> options = new FirebaseRecyclerOptions.Builder<People>()
.setQuery(query, People.class)
.build();
This correctly query all the people with national origin being "USA". However, what if I also want to sort the data alphabetically based on the name of the person before populating the recycle view. What is the best way of dong this?
Thanks in advance.
This question already has answers here:
Firebase Android - startAt() and endAt() not working correctly?
(2 answers)
Closed 3 years ago.
I have objects named "trips" saved in the firebase database, which can uniquely identify using seasonCode and TripCode combination.
Now I want to retrieve all the trips which include a particular seasonCode (ex: trips with seasonCode="SeasonCose1"). but need to extract data set chunk wise. (let's say 10 trips at a time)
FirebaseDatabase.getInstance(firebaseApp).getReference("resources").child("trips").orderByChild("seasonCode").equalTo("seasonCode1").limitToFirst(10)
I figured this query returns first 10 trips wich seasonCode="SeasonCode1". if I have last retrieved "Trip" object, how can I retrieve the next 10 trips with seasonCode="seasonCode1"?
Finally found a solution. I can get that through wildcard queries.
Firebase Android - startAt() and endAt() not working correctly?
This question already has answers here:
Google Firestore - How to get several documents by multiple ids in one round-trip?
(17 answers)
Closed 4 years ago.
I have below FireStore data
And I have a list specialUsers which is defined as
val specialUsers = arrayOf("stark#gmail.com", "lannester#gmail.com")
So I want to get collections which are in the specialUsers, something like
collection("users").whereIn(specialUsers)
But, couldn't find any documentation related to this. How can I perform above intention in Firestore?
As he says here in 6:19
There is still no way for the database to automatically grab specific user name and profile for each review as I requesting them. I would need to make separate database request for every single review I get to fetch this information and that's bad. so If we wont to automatically include information about who rote a particular review we will need to copy sample of the user profile to the particular review and this is the way (to brake data Normalization) specifically in could FireStore.
This question already has an answer here:
How to query a Firestore collection using a property from a step deeper with Kotlin? [duplicate]
(1 answer)
Closed 4 years ago.
I am new to Firestore cloud and I am trying to learn about it.
I am trying to build a project and I am stuck at one point.
My Firestore database looks like this:
enter image description here
enter image description here
I have a Groups collection that has "Users List" Sub-collection" where I have "timestamp" and "user_id".
When I created a group I grabbed the user_id of the "user" that creates a group and added that in "Users List".
Now I am looking to retrieve all the "Groups"(groupName) that the "CurrentUser" is involved in, and I am having hard time to figure it out because I found that NoSQL databases are very different from SQL databases.
Would be great if someone can shed a light on this matter.
Thank you
I'm not sure about this, but I think your best bet is to add some field to collect group that each user involve in "Users" Collection using array or object to collect it since Firestore can collect array or object.
But TBH, I not sure this is the best, but it is the best I can think rightnow. Because with these you only need to query 1 time for each user that you want to get usergroup and it help you to not use lots of query to get usergroups for each user. But it will couse you some additional size. But it still fine since Firestore costing is base on both query and size for this case. Little(?) additional data is better than waste lots of query to get usergroup for only one user.
This question already has answers here:
Firestore multiple range query
(3 answers)
Closed 3 years ago.
I know that basically performing range filters on multiple fields like the code below is not allowed:
eventRef
.whereField("dateTimeStart", isGreaterThan: startTime)
.whereField("dateTimeStart", isLessThan: finishTime)
.whereField("price", isGreaterThan: 10)
.whereField("price", isLessThan: 1000)
I have to choose between performing filter based on "dateTimeStart" or just "price" in one query, but I need to filter based on those two criteria, is there any trick to do this? I just can think that I have to do query based on price only and then the result of the query need to be sorted manually
Is there any better option than this approach?
When you are using the query in your code, you'll get for sure an error that sounds like this:
Caused by: java.lang.IllegalArgumentException: All where filters other than whereEqualTo() must be on the same field. But you have filters on 'dateTimeStart' and 'price'
So there is no way in Firestore to use whereEqualTo() function on more than one property. In this case, you should consider augmenting your data structure to allow a reverse lookup. So you should duplicate data by creating a new collection named priceAndPeriod in which you should add all your products that have the price between 10 and 1000 and also have the dateTimeStart between startTime and finishTime.
Note, there is no problem with duplicating data, 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. It is for Firebase Real-time database but same principle applies to Cloud Firestore.
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.