Reputation: 488
So I'm making a database for movies. I originally was going to embed the actors inside of the movie similar to this.
{
title : 'movie',
actors : [
{
name: 'actor',
DOB : '1/1/1',
},
{
name: 'actor2',
DOB : '1/1/1',
}
],
}
I quickly decided this was a poor decision. So my next idea was to create a separate collection of actors and then embed the actor id in the movie doc like this.
{
title : 'movie',
actors : [
'actorid1',
'actorid2',
],
}
Is this bad implementation? It seems like I would also have to make a list of movies in the actor doc if I wanted to keep track of all the movies an actor was in. Would it be better to just create a linking table to relate the two? I wasn't sure if I should avoid the related SQL structure for a NoSQL db. Are linking tables frowned upon in Mongodb?
Upvotes: 1
Views: 2373
Reputation: 1288
In general, using a link collection and doing joins to access data (relational data model) is possible in MongoDB. However it just uses different approach to model data. Representing data in BSON documents (Binary JSON) with sub-documents and arrays in many cases makes linking collections and joins redundant. Using the document model, embedded sub-documents and arrays effectively pre-JOIN data by aggregating related fields within a single data structure. Further, documents have couple of advantages over the relational data model. Firstly, there is no need to map a relational model to an object model that adds to complexity and may slow down development. Secondly, documents provide better performance and scalability as a whole document can be read from one physical location whether it is memory or disk and eliminate cross-node JOINs when a database is distributed across multiple nodes.
As for your particular case, I assume "Get all actors of the top 10 rated movies from rotten tomatoes" or vise versa "Get highest rated movies from these actors" are not the most frequent queries for a movies database. In my opinion, getting a movie along with starring actors or an actor with a list of movies are. Then I whould consider the following schema:
{
title : 'movie',
actors : [
{
_id: 'actor_id1',
name: 'actor1'
},
{
_id: 'actor_id'2,
name: 'actor2'
}
],
plot: '...',
reviews: [...],
...
}
{
name : 'actor1',
movies : [
{
_id: 'movie_id1',
name: "movie1'
},
{
_id: 'movie_id2',
name: "movie2'
}
],
biography: '...',
pictures: [...],
...
}
There are references in both directions from movies to actors and vise versa to aggregate information. Also there are actor and movie names along with corresponding _id to get this data in one request. As actor's names change not frequently as well as names of the movies there is a low chance to break consistency.
Upvotes: 3