Guest User

Untitled

a guest
Mar 18th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. var MovieSchema = new Schema({
  2. name: {
  3. type:String,
  4. required:true,
  5. minlength: [3, 'Movie Name must be at least 3 characters.'],
  6. },
  7. genres:[{
  8. type:Schema.Types.ObjectId,
  9. ref:'genres',
  10. }],
  11. })
  12. var GenreSchema = new Schema({
  13. name: {
  14. type:String,
  15. required:true,
  16. minlength: [3, 'Genre Name must be at least 3 characters.'],
  17. },
  18.  
  19. });
  20. Movies have genres. That's the main idea. What I wanna do is fetch movies and show genres for each movies.I can do it.
  21. but the thing gets complicated when I want to fetch movies based on genre. if you take a look at schemas, I don't know how to do it.
  22. so Is this a good idea that I add this movies:[{ type:Schema.Types.ObjectId, ref:'movies'}] in GenreSchema ? I know fetching
  23. movies based on genre would be super simple,but inserting gets complicated.
  24. when I save movie, I do this :
  25. for(var i=0;i<req.body.genres;i++){
  26. movie.genres.push(req.body.genres[i]);
  27. } now I also have to put this movie into those genres.
  28. then When I need to edit movie (which means maybe deleting movie from one of the genres, What I have to do is go to genres,
  29. find the genre ,that user deleted the movie from and genre.movies.delete(movie_id) (delete movie from that genre). you see
  30. how inserting,updating got complicated?
  31. updat
Add Comment
Please, Sign In to add comment