Search code examples
databasefirebasegoogle-cloud-firestorestructure

Best way of structuring documents in Cloud Firestore (NoSQL databases)?


I am trying to implement a Firestore Cloud DB but I am new to NoSQL databases.

I want to know whats the best way of arranging these sets into collections/documents:

I have restaurants which have different foods and reservations. What would be the best approach to structure these sets of data into Firestore DB?

Is this a right approach:

Restaurant1 (Collection)
----> Foods (document) 
----> Reservations (document)

Solution

  • According to your comment:

    the restaurant´s management should be able to see his reservation together with other reservations from other clients, in a list. And each client should be able to see his history of reservations as well.

    I'll try to provide you a schema that can help you get that data very easily.

    Firestore-rrot
       |
       --- users (collection)
       |    |
       |    --- uid (document)
       |    |    |
       |    |    --- type: "manager"
       |    |
       |    --- uid (document)
       |         |
       |         --- type: "client"
       |
       --- reservations (collection)
            |
            --- reservationIdOne (document)
            |       |
            |       --- reservedBy: "uid"
            |       |
            |       --- date: September 21, 2019 at 1:15:02 PM UTC+3
            |
            --- reservationIdTwo (document)
                    |
                    --- reservedBy: "uid"
                    |
                    --- date: September 21, 2019 at 1:18:42 PM UTC+3
    

    Using this schema, you can simply query the database to get all users or specific users (manager or clients). You can also get all reservations by adding a reference on reservations collection. If you want to get the reservation only of a single type (manager or client), you should use a query that might look like this:

    db.collection("reservations").whereEqual("type", "manager");
    

    As you can see, I have also added a date property so you can easily sort them descending (last reservation first).