0

I am trying to convert a relational MySQL database into a document database, as a mandatory school assignment.

As shown in the ER diagram the idea of the SQL database is that a user can be registered as a regular user and a restaurant, but not both. Users can then place orders on restaurants. The products of each order then gets stored in a junction table.

To convert this to a Document database (MongoDB) my idea is to have the following collections:

Users
orders:[{products:[{product},{product}]}]

Restaurants
orders:[{products:[{product},{product}]}]

Each time a new order is placed it will be saved in the users collection and restaurant collection.

But i am not sure if it is even necesarry to have both a Users and a Restaurants collection, or if you could just have Users and then specify if the user is a restaurant.

And i am also not sure how i can establish a relationship, so if a user deletes an order it will also be deleted in the Restaurants order array.

I recently failed my Database exam (among other things) because i had the structure of 3 collections like this:

Users
{_id etc, orders:[{_id: id of the order}]}

Restaurants
{_id etc, orders:[{_id: id of the order}]}

Orders:
{id etc, products: [{product},{product}]}

Here each time a user makes an order it is saved in the order collection and a _id is stored in the restaurants and users arrays.

I was told this is wrong, so how would be the correct way to design a Document Database What i am trying to achive is to store as much data in each collection but still have a connection between orders, restaurants and users so there will be no wrong data in the database.

enter image description here

2
  • 1
    Why are you trying to implement a clearly relational model in a non-relational database?
    – mustaccio
    Feb 7 at 12:58
  • @mustaccio Because my teacher told me to do so
    – Soma Juice
    Feb 7 at 13:00

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.