0

I am trying to figure out the best way to structure the data for my app. I want it so users have a list of trips and then can go to a detail view for expenses (fuel, hotel, food, misc).

The number of trips for a user will be a lot and this is just an example there will be about 20 items on the details page. I feel like I am overthinking it but not sure if having it read all 20 items of every trip in the list just to display 2 items for each trip is a good idea.

Not sure if it will make a difference but I am using Xcode 12.4

Would it be best to have the trip summary and then a subcollection of details?

users:
    - UID: "randomly_generated"
    - firstName: "James"
    - email: "a@email.com"

trips:
    - startDate: "2/5/2022"
    - destination: "Orlando, FL"
    - userID: "Users UID"
    - details: []
        - food: "150"
        - hotel: "400"
        - fuel: "250"
        - misc: "75"

or should I just have it all under trips?

users:
    - UID: "randomly_generated"
    - firstName: "James"
    - email: "a@email.com"

trips:
    - startDate: "2/5/2022"
    - destination: "Orlando, FL"
    - userID: "Users UID"
    - food: "150"
    - hotel: "400"
    - fuel: "250"
    - misc: "75"
OEZ
  • 55
  • 8
  • I'm not sure I understand. What exactly is wrong with your database schemas? – Alex Mamo Mar 15 '22 at 05:22
  • @AlexMamo I am trying to figure out which would be the correct or "best practice". I think either of them will work I just didn't know if one method would be better than the other. – OEZ Mar 15 '22 at 06:22
  • [There isn't a "best practice"](https://stackoverflow.com/questions/53053768/what-is-the-correct-way-to-structure-this-kind-of-data-in-firestore). We are usually structuring a Firestore database according to the queries that we want to perform. What are those queries? – Alex Mamo Mar 15 '22 at 06:38
  • I will have the list that displays Start Date, End Date and Destination... A Detail View that displays everything. Those will be the main 2 used.... I am also planning to have another screen where you can see total cost for all trips that quarter or year. – OEZ Mar 15 '22 at 06:42

1 Answers1

1

Try this:

  • users:

    • FirstUID
      • firstName: "James"
      • email: "a@email.com"
    • SecondUID
      • firstName: "John"
      • email: "b@email.com"
  • trips:

    • FirstTripId
      • startDate: "2/5/2022"
      • destination: "Orlando, FL"
      • uid: FirstUID
      • food: "150"
      • hotel: "400"
      • fuel: "250"
      • misc: "75"
    • SecondTripId
      • startDate: "3/5/2022"
      • destination: "Harare, Zimbabwe"
      • uid: SecondUID
      • food: "21"
      • hotel: "69"
      • fuel: "420"
      • misc: "7"

/// add this next part if you wish to do less queries or if you ever use rtdb

  • userTrips
    • FirstUID
      • FirstTripId
        • startDate: "2/5/2022"
      • NextTripIds
    • SecondUID
      • SecondTripId
        • startDate: "3/5/2022"

///

When querying with Firestore you can make direct composite queries in the trips child and specify the UID you want and it will retrieve all of them, additionally, you can limit the amount of trips you want to return

https://firebase.google.com/docs/firestore/query-data/queries

With RTDB, you cannot make multiple filter queries at the same time hence why you would need to add a separate userTrips child in order to filter out all the trips to a specific user

https://firebase.google.com/docs/database/ios/read-and-write

Denzel
  • 309
  • 1
  • 8