Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var MovieSchema = new Schema({
- name: {
- type:String,
- required:true,
- minlength: [3, 'Movie Name must be at least 3 characters.'],
- },
- genres:[{
- type:Schema.Types.ObjectId,
- ref:'genres',
- }],
- })
- var GenreSchema = new Schema({
- name: {
- type:String,
- required:true,
- minlength: [3, 'Genre Name must be at least 3 characters.'],
- },
- });
- 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.
- 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.
- so Is this a good idea that I add this movies:[{ type:Schema.Types.ObjectId, ref:'movies'}] in GenreSchema ? I know fetching
- movies based on genre would be super simple,but inserting gets complicated.
- when I save movie, I do this :
- for(var i=0;i<req.body.genres;i++){
- movie.genres.push(req.body.genres[i]);
- } now I also have to put this movie into those genres.
- 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,
- find the genre ,that user deleted the movie from and genre.movies.delete(movie_id) (delete movie from that genre). you see
- how inserting,updating got complicated?
- updat
Add Comment
Please, Sign In to add comment