0

In my web application made with Ionic and Firestore, I would like to select 5 random items to put on sale- The items are documents that are in a collection collectA . To do this I have seen that firebase does not allow to get documents in a random way, but a possible solution to get them is shown in this post Firestore: How to get random documents in a collection . But I would like 5 random items to be taken, then another 5 that have not already been taken, and so on. When all items have been taken, you start over. Would it be a good solution to consider a new collectB collection in which to insert the 5 randomly taken items and delete them from the collectA collection? Then do it for all the items taken. When collectA becomes empty and all items are in collectB, do the same process but from collectB to collectA. The disadvantage I think is only in the increased cost of the write-offs ($0.02 per 100,000). The writes are not because when I take the 5 random items I still have to modify some fields of these documents. I don't want the queries to slow down or the costs to increase a lot.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
sirss
  • 171
  • 1
  • 9

1 Answers1

2

If you need 5 new random items for all your users in the application, then don't do that operation in Firestore, do it in the Realtime Database, it's much cheaper for choosing such random items. Both databases are working really well together in the same project. That being said, you can have a structure that looks like this:

Firebase-root
  |
  --- products
  |     |
  |     --- $productId: true
  |     |
  |     --- $productId: true
  |
  --- consumedProducts
        |
        --- $productId: true
        |
        --- $productId: true

There are two solutions to this problem. Every time you get 5 new random IDs from the "products" node, add them also to the "consumedProducts" node. To be able not to choose the same IDs again, always check if the new IDs are not already present in the "consumedProducts" node. After a while, when the "consumedProducts" will contain the same IDs as the "products" node, then you can simply remove it and start over again. The second solution might be to add those 5 elements into the "consumedProducts" and right after that delete them from "products" node. When the "products" node remains empty, do the same thing with the "consumedProducts".

Now according to the logic of your app, you should decide which one is better to be used, but remember, always keep in sync, the actual products from Firestore with corresponding IDs in the Realtime Database. For instance, if you add a new product in Firestore, add the corresponding ID in the Realtime Database node. That should happen also when you delete a product from Firestore.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Hi Alex, thanks for the answer. So, I use the Firestore database and every user who creates a new item also loads images and videos of the item. So in the related document there are also url fields linked to videos/images saved in the firebase cloud storage. If I understand correctly, then, do you suggest to use both databases and when a user creates a new element, only the id is saved in the realtime datatabase? Then on the latter I take the documents at random ? – sirss Nov 30 '21 at 18:50
  • However, once the 5 items are selected, these documents will undergo changes (writing costs). So I don't understand what is the advantage of using 2 databases in terms of cost? – sirss Nov 30 '21 at 18:56
  • Yes, that's correct, only save the ID in the Realtime Database. Then get the random IDs, and right after that create a Firebase call to get the actual documents. The advantage of using the second database is that you aren't charged for the number of read operations you perform, rather on the amount of data you download. Since you are only downloading a small amount of data will much cheaper than using Firestore, where you pay for every read operation. Or if you want you can still use Firestore, then store all IDs in a single document. It's up to you to decide which is better for your use case. – Alex Mamo Nov 30 '21 at 20:03
  • 1
    perfect, yes you are right, I didn't think of that. I save a lot by using realtime db, considering I only download id =) . I will follow this path, thanks a lot – sirss Nov 30 '21 at 22:14