Firestore Food-Ordering Application Database Design Questions

1,569

There is no certain way to answer this. But there are some rules I would say to tackle this efficiently.

  1. Put Data in a same document if you want to show it together. (Not too big, neither too small)
  2. Put data in collections, when you want to search an individual piece of that data, or you have a database to grow.
  3. Use map if you want to search a parameter based on that data.
  4. use map if you want to store related data ( Like delivery addresses of the user).
  5. Document write doesn't count on data you wrote, no matter if you increase your order counter value by 1, or change the whole document, it will count as one write.
Share:
1,569
ahmetakil
Author by

ahmetakil

Updated on December 18, 2022

Comments

  • ahmetakil
    ahmetakil over 1 year

    I'm working on a Flutter application that basically allows users to place orders to restaurants then go and pick-up those orders.

    A restaurant has a List of MenuGroups and each group has a List of ExtraIngredients and List of MenuItems.

    A MenuItem has several variants with different prices also List of Ingredients that come with that item and ExtraIngridients that can be added.

    Currently, in firestore I've a collection called restaurants and each restaurant has a List of MenuGroups. Is there a way to make this more efficient

    For example, is it better to do the menuGroups as a subcollection in the document?

    Firestore design

    Also to implement an order queue number system (first order starts from 1 goes to 99 then goes back to 1)

    Is it better to store that in a variable in restaurant document(Whenever there is a new order there will be 1 read to get the current number than 1 write to increase that number and also after reaching 99 to set it back to 1)

    or in the order document itself (Now each order has an extra field 1 read to get the last order's number and the new order will be written all together so there is no extra write operation just for the queue number)

  • ahmetakil
    ahmetakil over 4 years
    So in order to implement order numbers, I was thinking to put orderNumber field in orders collection so each order document has a field that starts from 1 and goes to 99 but then whenever a new order arrives i need to find out what was the last order number and the best way i can think of right now is to go through all of the orders and find the most recent order to that restaurant. Is there a better way to handle this ?
  • meditat
    meditat over 4 years
    depends on how you have structured your database. if this is kind of a server thing that you don't want to perform on a client side, consider using cloud functions.
  • meditat
    meditat over 4 years
    You can post how you have structured the Orders collection, then only I could suggest something.