In android, I have a list generated by user input which needs to be compared to a list in Firebase Realtime Database. I want to compare if List 1 contains all the elements in List 2.
This data then needs to be displayed in FirebaseRecyclerPagingAdapter.
Is there anyway this can be achieved?
There is no way to compare two lists in a single operation on the Firebase Realtime Database (nor on Cloud Firestore for those wondering). You will have to load the relevant lists from the database and perform the comparison inside your application code.
Related
In my chat application, I store the participants of a chat as their UIDs in a Map so I can so I can do queries like this:
.whereEqualTo("participantUIDs.$currentUserUid", true)
.whereEqualTo("participantUIDs.$partnerUid", true)
The problem is when I try to use this with orderBy
.whereEqualTo("participantUIDs.$currentUserUid", true)
.orderBy("lastMessageSentTimestamp")
I have to create a custom index. But this index will contain that specific user UID and I can't create an index for every user in my app. How can I circumvent this problem?
You can order the documents on the client after an unordered query. This should not be very taxing on the client app when the number of documents is less than 10,000.
Regarding:
I can't create an index for every user in my app.
That's definitely not an option, as there are some limitations when it comes to Cloud Firestore indexes:
https://firebase.google.com/docs/firestore/quotas#indexes
However, even if you manage to stay below these limits, that's not an option to manually create an index for each and every user that joins your app.
In my opinion, for your particular use-case, you should consider augmenting your data structure to allow a reverse lookup. Meaning that you should create a participantUIDs collection where you should keep the lists for each user. This technique is called denormalization and is a common practice when it comes to NoSQL databases like Cloud Firestore or Firebase Realtime Database.
But remember, there is "no perfect database structure":
What is the correct way to structure this kind of data in Firestore?
It's a little old, but I think this video might also help:
https://www.youtube.com/watch?v=u3KwKQddPoo
More info regarding why you need an index:
Why does this firestore query require an index?
P.S. You can also rely on Firebase Realtime Database when Cloud Firestore may become a little expensive. Both work really well together.
Info:
Array or Subcollection for storing events user uploaded
I am using firebase Cloud Firestore for my Android and iOS apps. I am stuck with how to structure my database.
Basically I have an "organization" collection that contains many users, and for every user, I want to save attendance time and leave time for every day.
The problem is I want to generate reports that allow me to get every day-attendance for each user, single-day attendance for all users, and all days attendance for all users.
so I tried this: inside the user I would have an "attendance" collection then each document is Unix timestamp of that day (to make sure that it's unique). then save fields like attend_time and leave_time...etc. the path is like that "/organization/android/users/3PRs42gRFzZQhLKcUhpf4wPMRV43/attendance/1590271200"
then I needed to get attendance for a single day for all users, so I did this: now I have another path "/organization/android/attendance" and inside the attendance, I store the Unix timestamp of the day, then the user ID then his attendance. and now I am saving attendance twice.
but I still can't get attendance for all days for all users!
this would be easy in Relational Database like SQL. any idea how to do it in firebase?
If you want to track attendance across all users, you're looking for a collection group query. This allows you to query documents from all collections named attendance.
Since a query in Firestore can only see data in the collection(s) it queries, you may have to duplicate some data from parent collections (your users and organizations) into each attendance document to allow the query. This type of data duplication is quite normal in NoSQL databases too.
Finally: if you need to perform many ad-hoc queries, you might want to consider using a database for those that is more suited to that use-case. For example, it is quite common to use Firestore to handle the direct-to-client interactions that require scaling to massive number of users, but then use BigQuery for the ad-hoc querying of that data. There is even a Firebase Extension that automatically exports to BigQuery to make this easier.
Hello fellow developers,
I have a question about getting the data from Firebase. Trying to compare the transactions list of one user with another user in a function. Two separate hashmaps are used to store the data (transaction list) of each user. Then I loop through the list of records of first user and try to match with the records of the second user. Since Firebase is asynchronous, function skips before I get the data. I need some ideas to manage the asynchronous fetching of data from Firebase.
Thanks
I have a Collection of Lists in which I store a bunch of documents. Each document contains a few details and a Collection of users. I store the users within the users Collection using the uid as a key and a boolean as a value. This means that only those users will be able to read that particular list. This is my database structure.
In my code I use this query:
FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
CollectionReference listsRef = rootRef.collection("Lists");
Query query = listsRef.orderBy("date").whereEqualTo("users."+uid, true);
And I get the following error:
FAILED_PRECONDITION: The query requires an index. You can create it here: ...
If I go to the Console to create the required index, everything works fine. But is there a possibility to create the indexes automatically, because I cannot create manually an index for each user that joins my app. Is there a problem if I have a few sorting indexes for each user?
Is there another workaround?
Thanks in advance!
Cloud Firestore automatically creates indexes on individual fields of your documents. Complex indexes that span multiple fields can (currently) only be created manually in the Firebase console.
Also note that not all queries need a unique index, as Firestore may be able to merge indexes (especially) for more complex queries. This can drastically reduce the number of unique indexes you need.
If you find that you need to create indexes programmatically, you typically should consider augmenting your data structure to allow a reverse lookup. For example, in your scenario, I'd add a userLists collection where you keep the lists for each user.
I need to get the specific child values only using fire base.for example in mysql:
Select table_name.title from table_name
How can I achieve it in fire base using android ?
When loading data from the Firebase Database, you're always retrieving full nodes. So if you want to retrieve a subset from the data, you'll either have to do it client-side or (more likely) create an extra list in the database with just the data you want to load.