What's the most 'mongo' way of representing many-to-many relationships that have attributes?
So for example:
Intro
MYSQL tables
people
=> firstName, lastName, ...
Movies
=> name, length ..
peopleMovies
=> movieId, personId, language, role
Solution 1
Embed people into movies...?
In MongoDB I understand it's good to denormalize and embed
but I don't want to embed
people into movies, it just doesn't logically make any sense. Because people don't necessarily only have to belongs to movies.
Solution 2
People
and Movies
will be two separate collections.
People
=> embed [{movieId: 12, personId: 1, language: "English", role: "Main"} ...]
Movies
=> embed [{movieId: 12, personId: 1, language: "English", role: "Main"} ...]
The issue with this solution is that when we want to update a person's role
for a specific movie
we'll need to run two update queries to ensure data is in sync in both collections.
Solution 3
We can also do something much more relational like and end up with three collections
People
=> firstName, lastName, ...
Movies
=> name, length ..
Castings
=> movieId, personId, language, role
The issue with this is that because of the lack of a join statement in MongoDB, it would take 3 queries
to go from people -> movies and vice versa.
Here is my question, what are some other ways to model something like this in MongoDB
and in a more NoSQL
way. In terms of the solutions provided, which one would be the best in terms of performance and convention in mongo.
See Question&Answers more detail:
os