MongoDB approach vs. relational approach

51 views
Skip to first unread message

Lazar Gubezkis

unread,
Sep 17, 2015, 4:06:43 PM9/17/15
to mongodb-user
Say I have a collection that contains a list of people and their cars (one car per person): 

{name:"Jack",car:"Honda"}

In a relational database, I would keep a list of cars in a separate table and reference the cars by their ids. Some people told me that in MongoDB  you simply write the car name as string in the document(as I wrote above). But that seems strange to me...What if I have a typo in the car name? What if I have several typos in the car name and I don't even remember what they are? I understand that you can also reference the car by its id, but that's a relational approach, whereas MongoDB isn't relational. What is the MongoDB way of handling this?

Thanks.

Wan Bachtiar

unread,
Sep 30, 2015, 3:03:59 AM9/30/15
to mongodb-user

Hi Lazar,


I understand that you can also reference the car by its id, but that’s a relational approach, whereas MongoDB isn’t relational. What is the MongoDB way of handling this?

One of the advantages of using MongoDB is that it allows you to focus on your application design and let the database design conform for the benefit of the application (See Domain Driven Design approach).

There are various data modelling patterns that you can employ to support your application design:

When designing the data models, always consider the application usage of the data (i.e. queries and data processing) as well as the inherent structure of the data itself.

If your application design requires better perfomance for read operations, as well as the ability to request and retrieve related data in a single operation then perhaps Embedded Data Model design would be suitable.

On the other hand, when embedding would result in duplication of data but also would not provide sufficient read performance advantages to outweigh the implications of the duplication, then you can model the schema as One-To-Many Relationships using document references.


What if I have a typo in the car name? What if I have several typos in the car name and I don’t even remember what they are?

Consistency for input data is normally handled in the application layer. In some cases where it makes sense to be handled in the database layer, you can store the data in separate documents to describe the relationships using references.


There is a dedicated section on MongoDB Data Modelling that you might find useful.

Please let us know if you have any questions.

Regards,

Wan


Reply all
Reply to author
Forward
0 new messages