Mysql – MongoDB document insertion and referencing

database-designmongodbMySQL

I am new in mongo and I have a sql background. I am currently working on a project about migrating data from mysql to mongo. I have transformed my data into json. here is a sample.

{
"IMDBURL":"http:\/\/us.imdb.com\/M\/title-     

 exact?Schrei%20aus%20Stein%20(1991)",
"release_date":"08-Mar-1996",
 "ratings":[{"user_id":916,"rating":3,"timestamp":"880845755"}],
 "genre":["Drama"],"video":"",
 "_id":1682,
 "title":"Scream of Stone (Schrei aus Stein) (1991)"
}

this is the first type of documents that I insert.Also in the same collection I insert some documents like this below. As i have read it is possible in mongo to insert different types of documents in the same collection

{
"zipcode":"77841",
"occupation":"student",
"gender":"M",
"_id":943,
"age":22
}

the user_id in the first document references to the _id in the second document.Because i insert many documents from each type I thought it would better to insert user informations on different documents and refer to them using their id. After reading the mongo documentation I am a little confused. Is this the best way to model the data or should I insert the second type of documents (user details ) on different collection. Also if I want to retrieve information of users or a user that makes a rating to a certain minutes how I am going to do it?

Best Answer

Why not put it all in the same document? You could also put it in a different collection. One way or another since you don't have any schemas to update, just try whatever suits your case. If this is a many to many relationship (like films-actors) then embedding everything wouldn't really work. You would have to reference other documents.

Film Collection:
    {
      {
          film1: ....
          actors: [actor_ids]
      },
      etc...
    }

Actor Collection:
{
     {  
          actor1:....
          actor_id: objectId
      },
     etc...
}

https://docs.mongodb.com/manual/core/data-model-design/