Randomly pairing users in Firebase [closed] - firebase

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I'm developing an application using Flutter and Firebase and I have to solve a problem that sounds like this:
each user (at any time) can put himself in a waiting list
Firebase has to handle the waiting list by creating (randomly) pairs of users
My idea is to have a collection in the Cloud Firestore for the waiting list in which each user is a document and a cloud function that reacts to the document creation by searching for an other user (document) in the collection, deleting both documents (i.e. removing the users from the waiting list) and creating another document in another collection that represents the pair.
The main issue is to design (if possible) a transaction in order to guarantee that two or more executions of the same cloud function do not overlap.
Any suggestion? Or any other way to solve this problem?
Thanks!

You could put all the users in a single document with Unique ID and data like following:
Then you could use a Cloud Function and that will execute following things:
Retrieving the Array called waiting from a specific document. It
will add new account user id to that array.
If the array has got at least 2 elements. It will add a new
array of maps named paired from the 2 elements from the waiting list and
add the value to firestore. It will call the existing paired
array and add the new 2 elements and save it to Firestore. Also update the paired_with field in
users doc.
It will remove the 2 elements from waiting list update an empty
list.

Related

Firebase Cloud Firestore or Realtime database in terms of pricing for my chat app? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 1 year ago.
Improve this question
I have been developing an app which has a feature to chat with other users. I have been been really confused for the past few days trying to find out which would be the better database to use in terms of pricing, I am talking about going at scale here.
Whenever a user sends a message he creates a document and while doing so I am also checking if the other user is online or not, for which I read another document. On average I have to make around 7-8 writes per message and 8-10 reads per message. Also whenever a user opens a conversation he sees the last 15 messages, and if he scrolls then he sees more. This increases the reads as well.
Also I want to know if I send a message and the other user gets it , who previously read 15 documents to see the last 15 messages , when I send a new message , the 15th message gets replaced with the new one , so do I get charged for the 15 document reads again?
Pricing is the main concern here, please help me find the best approach here.
Firebase has a database recommender and a pricing calculator in its documentation to help you answer this question. I'd expect it to point to the Realtime Database here, primarily based on the fact that you'll have many smaller write operations.
For your second question (please limit yourself to a single question per post going forward): if your listener remains active, or if your local cache already contains the 14 unmodified documents, you will be charged only one document read (for the new document that needs to be read on the server to return it to the client).

How to get the collection ID and then create the subcollection for firestore [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I currently doing an rent app. Then I stuck in create a subcollection for each user. I create a booking details and desire to put it as a subcollection but it failed. I have read through the official documentation, but it provide fews example only. So, how do I create a subcollection?
If you are asking how to create a collection in the console then on the right side of your screenshot there's a button that says + Start collection. Clicking that will create a 'sub' collection inside the selected document.
If you're asking how to create it in code, it automatic!
See Subcollections to start with and then see the more complete documentation on Collections which says
You do not need to "create" or "delete" collections. After you create
the first document in a collection, the collection exists. If you
delete all of the documents in a collection, it no longer exists.
So the short answer is; creating a document within a collection also creates the collection - in short some pseudo code
let myDocRef = db.collection("users").document("my_user_id")
will create (when written to) a collection called "users" with a single document "my_user_id"
if you then do this
let subCollectionRef = myDocRef.collection("sub_collection").document("doc")
will create a hierarchy of
users (a collection)
my_user_id (a document)
sub_collection (a collection)
doc (a document)
Collections are always paired with documents in even sections
collection/document/collection/document
------ -----
pair pair

Firestore contacts list data model [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am working on a management app for local groups.
The app also displays each group its contacts list.
I wonder how should I design my database?
If I store all users on a root level collection, each time a user opens the contacts list it will read all his group's users' documents. Assuming 1000 contacts groups, for example, adds up to A LOT of reads for a simple task like that.
The same goes for using a subcollection for the group.
Storing the group's contacts in the group's document as a map is not scalable.
And the same goes for using a map in the group's document to index a collection or a subcollection in a denormalized model.
And this is where I'm stuck.
Am I missing something or that these are the options I have and I need to choose between a large number of reads each time a user opens the contacts list and a model designed for limited sized groups?
I think this is a common situation. Many items are related to fewer parent items, and we want to show the parent item's children.
For Firestore, it's recommended that you limit your queries.
For example, if a user wants to see the groups' contact list, that's fine, make a query. But don't show all 1000+ contacts at once. Even Facebook doesn't do that (go to a Facebook group and click on "Members", you'll only be shown a dozen or so, and as you scroll down, more queries are made to show you more.)
Users expect this kind of behavior: lazy loading or pagination.
Documentation for Firestore to limit/paginate queries: limit, paginate
This way, groups can be documents, and contacts can be documents that have an Array field that contains the group-id's they belong to. To display a group's contacts, you can use an "array-contains" query on the contacts collection.
Something like this
contacts (collection)
|
--- {contactId} (document)
|
--- (...contact data...)
|
--- group_membership (array)
|
0--- 'groupId111'
|
1--- 'groupId222'
(For the contacts(col)/contact(doc)/group_membership(field) I'm assuming a contact can belong to multiple groups if this is not the case, the field can be a string instead of an array, but it's safer to make it an array in case you change your mind.)
All I'm getting at here is that your vision for the db structure is fine and common, you don't have to restructure just to avoid querying a thousand contact documents... because you don't have to. You don't have to query 100 times the amount of data that your end-user will actually need.

how to query in flutter cloud firestore with multiple conditions(multiple where clauses)? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
widget.filteredRef= FirebaseFirestore.instance
.collection('users')
.doc(widget.uid)
.collection('incomes')
.where('date',isGreaterThanOrEqualTo: startTimeStamp,isLessThanOrEqualTo: endTimeStamp)
.where('amount',isGreaterThanOrEqualTo: amountFrom,isLessThanOrEqualTo: amountTo)
.snapshots();
Hi, I'm new to flutter. I want to specify a query to retrieve income data from specific range of date and amount of the users as a list from flutter cloud firestore. Example, Date from 01/01/2020 to 01/02/2020 of amount 0 to 1000. But using two where clause or two orderby or one orderby with one where clause is not working. Please tell me the way get as a stream.
Here is my firestore income document link:
https://drive.google.com/file/d/1lAbF1t641nBK4hRn5zLT-4eNUQ-FyajW/view?usp=sharing
You will need to create an Index in the Firebase Console itself. This is because firebase stores basically long arrays of the documents, and does something like this: Query = CorrectIndex[start:end], which it cannot do if the data is not sorted. It makes such indices for every item of the document, but not for any combinations (That would be to expensive for things you probably do not need). So, you need to tell Firebase that it needs to make an correctly sorted index for every combination you want to use, so it can be fast when you start querying. If I am not mistaken, in your error message a link to your firebase console is already given. Click that, and you will be guided through the process.
It is, however, not completely correct. You will need to remake that index to use a "CollectionGroup". Everything else can stay the same. Anyway, some good series about firebase is this: https://www.youtube.com/watch?v=Ofux_4c94FI.

Firebase Firestore huge amount of reading document concern [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
EDIT: Basically do you get charge for the documents you query. The answers is no, if you query 100K documents but only get back 10 you only get charge for the 10 documents you get form your query.
Hi so I have been using firebase firestore and it's been great. However, I have some questions about it. Currently, I'm working on an app where users can place orders and by default, the 'isActive' property is true so that the admin can see the orders. When the order is completed the property turns false and doesn't show up. However, eventually, I will accumulate thousands of orders and my question is will I get charged for the documents that I read that is true or it will counts as reading all thousands of documents even though I don't use it.
You will most definitely be charged for stored data regardless of if you access it your not. If you do decide to access it you'll be charged for that as well. Fortunately you have 20K free reads a day and then you pay after that.
While I think that answers your question. If your app reads in 20,000 documents in one action (open a page and then it loads in everything) that's not going to scale well for you and that's a lot of data. Which means your client devices will have to process 20K documents which is not ideal.
I'd advise using limit and then do pagination or infinite scrolling.
It's fairly simple: if your document is read from/on the server, you will be charged for a document read.
If your clients are not requesting documents with isActive is false, they won't generate document read charges for those documents. I your admin is reading those documents, they will generate read charges for those documents.

Resources