1

I want to store user specific data in mongodb. For example: User creates an account with email id and password and stores his favorite movie names with genre and year. one user can have many movie names stored like this. example

{
"email":"contact@proboticslab.com",
"password":"something",
"movies":[
  {
  "name":"avatar",
  "genre":"action",
  "year":"2012"
  },
  {
  "name":"black panther",
  "genre":"action",
  "year":"2018"
  },
  {
  "name":"ironman",
  "genre":"action",
  "year":"2016"
  }
]
}

so my question is, is it a right way to store data in such manner or should I follow different patter? There will be more users not just one. please explain me with example if possible. I am new to mongo db. Thank you.

Saravana
  • 12,647
  • 2
  • 39
  • 57
  • 2
    Your database structure is up to you. I would suggest to have a user model and a movie model. – Striped Feb 25 '18 at 10:39
  • 1
    It would be interesting to know if mongoDB does minimize duplicate movies or not. However the better way would definetly be storing all the movies in a separate collection, then store only their _id in the user. – Jonas Wilms Feb 25 '18 at 10:45
  • As already mentioned, a table of movies and a table of users then link the two via a common column, using the movie_id column and a user_movies comma separated column, or a table of users_to_movies. There are a few different ways and you would need to see what is best for you. – Dan_ Feb 25 '18 at 10:46
  • This is either to broad or to opinion based, or it is a good canonical (if one does not exist already). – Jonas Wilms Feb 25 '18 at 10:50
  • I found a really good [explanation on MongoDB design](https://docs.mongodb.com/manual/core/data-model-design/). Give it a go :) – Jonas Wilms Feb 25 '18 at 11:33

0 Answers0