{name:"Jack",car:"Honda"}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