 Hey what is going on everybody my name is Roddy and you're watching my channel Roddy the Brand today we're going to create a RESTful API using Node.js, Express and MongoDB if you found the video useful consider liking it subscribe to my channel for more videos like this and give me a comment below now let's jump on a computer and get started welcome everybody and let's get started the first thing that I'm going to do is create a new project folder and I'm going to call this REST Movie API let's enter this folder and create a new project and to do this I'm going to do left shift right click and open power share window here this will basically cd to my project folder that we just created and the first thing that we need to do is initialize a new project by doing npm init and then dash y to skip some of the questions this should take a second to do and as you can see it's created a default project for us which we can start using if you look in the folder you should have this package.json file and we should be good to go now the first thing that I'm going to do is install all of the dependencies that are required for this project starting with npm i for install and then we're gonna have course and course is basically allow all api to be accessible from all origins but of course you can limit it to a specific one if you wish to .emv is the other dependency that we're going to use and this dependency is basically is going to allow us to store variables database strings and so on we have express as we're going to be using express to start an express server today and finally we have mongoose with a database if you press enter this should take a couple of seconds and the next thing that we need to do is do npm install again and we need to do dash dash save dash def and i'm going to install nodemon as a development dependency just because i don't want to be restarting the server manually every time we make a change this is what nodemon is going to do for us now i'm going to open the project in visual studio code but feel free to use whatever code editor you wish i doesn't really matter all you need to do is go to file and open your project folder that's what i have here at the moment and the first thing that we're going to look at is the package dot jason file now inside the pack dot jason file we have all of the dependencies that we installed here depending on when you're watching this video the versions of those dependencies might differ but the other thing that you need to notice is that we have the development dependency here and that is nodemon i want to start my project nodemon to rene so what i'm going to do is copy nodemon and inside scripts i'm going to create another line this one is going to be called start column and then i'm going to put nodemon and then the app that i want to start is going to be called app.js which we're going to create next let's save this close package dot jason and let's start by creating or app.js file now this is going to be our main file for the project and everything else will be kind of linked to this so in here we need to include some of the dependencies that we need we need to set up some of the middleware and we need to obviously start an express server to do this let's start by including some of the dependencies so const express is the first one and and that would be equal require and then we require express like so then let's do const course which is going to be equals require and then we require course we also need to require dot env but dot env is slightly different we need to require it by doing require and in single quotes we can do dot env like so and then we need to put dot config and close this that's it now we need to create an express server and to do this i'm going to do const app equals express and now i can use this app to listen to a port number which we need to create first of all so const port and this is going to be equals we can either put a default port or 5000 or whatever you wish whatever is available on your machine and if you want to publish your api somewhere you might want to add a environment port so to do that you can just do process dot env and then port or 5000 that's it now let's set up some of the middleware the first one is course so euro is usable on all origins so app dot use and we're going to use course close open close and that's it okay now we need to set two more middlewares and the first one is app dot use express dot jason and if you want to have a look at what this does you can hover over and this is going to give you a small description so returns middleware that only passes jason and only looks at requests where the content type header matches the type option and then we need another one app dot use and this one is express dot euro encoded and for euro encoded we just need to put one option which is extended to true like so let's close this and if you want to know a little bit more about this this is a middleware that passes euro encoded bodies i'm actually going to create my routes in a different file just so we're a little bit more organized but you could potentially list them in here if you wish to so let's do a totally different file and to do this i'm going to do const route equals require and then we're going to require the file which we haven't yet created but we will do in a second let's do server routes and then i'm going to call this something like movyroute to use those routes we have to do app dot use and then inside here we can just put slash and comma and then we use the routes that same and the last thing that we need to set up here is all up to listen on the port that we created so what we have to do is app dot listen and we pass the port number so port which we created here and then we can just console log something helpful so something like console a log and in single slanted quotes i can do something like listening on port and then with dollar sign and curly brackets i can pass the port number like so we are done with the app dot js file and we might as well create the whole project structure now and start building everything one by one the first thing that i'm going to do is create all dot env file dot env this is going to hold all variables then i'm going to create a new folder called server so server and this folder is going to contain all controllers this is also going to control all models and this is also going to control all routes okay inside the controllers let's create a controller called movy controller and this is going to be a js file in models we're going to have two so the first one is going to be called db for database dot js and this is where we're going to add all of the mogu db settings so we can connect to the database and we can reuse this file anywhere we wish really and the other file that i need to create inside models is the movyschema so what i'm going to do is call it movy dot js that's absolutely fine inside routes we need to just create all routes file so i'm going to call this one something like movyrout.js and that should be absolutely fine now i think let's start by building a simple route and a simple controller and then we're going to look into the how to connect to the database and we can start building our project first of all close everything except movyrout and in order to be able to use the express router we're going to have to bring express again so let's do const express equals require and then we require express just like before and then to bring the router we can do const router and now we use express from here so router equals express dot router okay now the next thing that i want to do is bring our controllers so basically all routes are going to be listed here but each route is going to be linked to a controller and the controller is basically going to do all the queries for us to do that i'm going to do const and let's call it movy controller and this is going to be equals require and then inside here we require the folder which is two dots back controllers then movy controller we don't have to specify the file here so i'm just going to leave it as this and close and then inside here we can start listing the app routes so let's do something like one more routes inside here we're going to list the app routes but before we do any of the routes a very important thing that we need to do is export this router now to do this we can do module dot export equals router like so and now hopefully if you go back to app.js this is what we've done we've linked all routes here app.use is using all routes and we've linked them in here and we've actually exported that so that can be used let's build our first route and let's build our first controller just so we can test the application before we start building anything else under here under app routes let's build something simple to build a new route we can use the router dot and then the first one is going to be get so the request for this one is going to be get but we're going to also use post patch and delete later on now inside here this is pretty much what the euro you want it to be and if you leave it a slash this is going to be kind of like the home page so it's going to be localhost 5000 and that's it that's going to be the home page but what I'm going to do in this video is I'm just going to do it a little bit more specific and I'm going to do an API slash movies so if I go to this route on the web browser it's basically going to hit this router and I want to link a controller so the controller can do all the database stuff and rendering as well so to do this I can do comma use the movie controller that we've linked like so and then I can do maybe let's call this one list movies something descriptive so we know where it is and close it now we need to create this controller here list movies to do this let's go to the explorer and open movie controller and let's build one and then we'll build the whole page later on so let's do first of all let's do exports dot list movies which we just created and this is going to be an asynchronous function because we're going to be using try and catch and this is going to have request and response and then after that this is going to be a arrow function and in curly brackets this is where our logic is going to be now the first thing that I want to do is just test something very simple so I'm going to do rest dot send and I'm going to send hello well like so and close so hopefully if we start our application which we can do from the power shell and to do this we can just do npm start add this just start to application if you get the node one starting note up dot the js and then listening on port 5000 this means that all up is working and if I go to the browser quickly so let me open it so if we go to the browser super quickly local hose 5000 we obviously don't have this route here we haven't created the but the one that we created is api slash movies and that's it so I've zoomed in a little bit so you can see but we are getting hello world which means that all router is working and if I do one two three save this not motion reset automatically and if I restart the page refresh sorry you will see that we're getting hello world one two three okay this means that our application is working and now might be a good time to connect to your database and do offer a query okay so the couple of ways that we can do this I'm actually going to use the atlas which is a cloud version of MongoDB but you can also use a local one and if you want to do that you can also do that and still follow along you will need to go to docs dot MongoDB dot com slash manual slash installation and install this in order to use the local one and also if you want to use the local one a great tool is MongoDB compass community it's a really good tool you can actually use it locally or you can connect to your or you can connect your cloud cluster as you can see I've done it before and this will basically list all of your databases and you can browse through them and so once I'm actually going to use the cloud today but saying this I will show you how to do the local one as well if you wish let's go back to MongoDB obviously you need to have an account I'm using a free shared cluster you can just create one from here if you haven't yet created one and a very important thing that you need to do is give your database access now what this does is pretty much you just gonna have to create a username and a password make a note of the password because we're gonna need it shortly and the other very important thing that you're gonna need to do is to give yourself a network access now if you go on this this is pretty much going to ask you to give ip access and all you need to do is add your local ip and then you can start and you can connect to at last and start using it that's pretty much it they make it super easy you can just click network access add new ip and that's pretty much it then for the database access you just add a new database user make note of the password because we're gonna need it in a second okay now if we jump back to databases the first thing that we need to do is go to connect and click on connect your application this will give you a string which we need to copy so I'm gonna copy this go back to a project and open the explorer and look for .emv this is going to hold all variables so I'm gonna create my first variable which is going to be called mongo db underscore ur right and I'm gonna put this as equals to the string that we just copied now the two important things that you need to do here this is the username that I have created and I need to paste my password as well so this is where you get it from the database access once you do this enter your password here I'm gonna replace it in a second and the other thing that you need to notice is the database name this is the default database name my first database but I want to change this to something like movies so that's another important thing to look at and let me just replace my password and I'm probably gonna hide it from you and if you wish to use the local version of mongo db what you can do is we can create another variable let's do mongo db ur right and I'm just gonna do it underscore local and in order to use that you can just do mongo db column slash slash and then localhost and then the database name which in this case I'm gonna put as just so we know and I'm not gonna use this but this is how you'd use it and I'm gonna show you how how this is gonna work out in a second so let me say this I'm gonna close it I'm gonna close everything else and the first thing now we need to do is jump back to models db.js and we need to set up or database connection or if it fails we need to we need to display an error to do this first of all we need to bring mongoose so const mongoose this is gonna be equals require and then I'm gonna require mongoose like so close this and then we need to do mongoose connect and then inside here we can bring the variable name that we just created and to do this we can do process dot env dot mongo db underscore ur i this is literally the variable name that we just created in dot env so this is how I'm gonna grab it and if you want to use the local one if you have installed it you can just do underscore local and that should work as well and now the next bit would be we need to put two options the first one is use new url faster and set this to true and the second one is use use unified topology and that needs to be set to true as well and close and now we can do const database equals mongo db mongoose dot connection and now we can check whether we have a connection on so we can do db dot on error then we can console log something and I think this is the example from mongo db so we can do console dot error dot bind and then inside here console cover connection error and if we have a successful connection we can do database dot ones and if you connect to the database we can do database dot ones and then inside here open function and with curly brackets close this and with curly brackets inside we can just do console dot log and then we can say connected like so and that's it now we can reuse this database file in our controllers we only have one controller but if you have multiple you can just reuse it which is great save this jump back to controllers and move your controller and the first thing that I'm going to do is require the database so require and then inside here dot dot dash and then models and then database like so close this and the next thing that we need to do is create and require or movie model to do this let's first of all require right so I'm gonna do so I'm gonna do const and then movie and then this is gonna be equals require and then we're gonna require dot dot slash models slash movie let's create this file now this is gonna be the movie schema so in models oh I've already created it so in models movie.js is the one that we want and this is where we're gonna do the movie's schema I'm gonna keep this very basic for this tutorial so we're only gonna have a few properties such as name description category and thumbnail first of all we need to include mongoose in here so let's do const mongoose equals require and then we require mongoose like so and then we can do const before you write anything else I'd like to export the mongoose model and to do this we can do module dot export equals and then mongoose dot model which we're gonna create now and then the model is going to code movie and then this is going to be movie schema like so let's create this movie schema now and to do this we can do const movie schema is equals new mongoose dot schema and then inside here with curly brackets we can start creating the schema okay so let's start by creating a name so every single movie is gonna have a name and this is gonna be a type of string and if you want to you can have it as required so you can either do required like so this is required or you can do it let's create the description now and for the description let's do type of string and let's do required you can either do it required set to true and that would also work did I do something okay I've missed a comma here so you definitely need a comma description and let's create the other two the next one is going to be category and it's going to be slightly different because I want to be able to have multiple categories so let's do comma first category and this is going to be type of array and then this can be required is what so true like so and the last one is going to be thumbnail so potentially I can just copy one of these because thumbnail is going to be a type of string so thumbnail type of string required this field is required that's absolutely fine I'm just going to do it like this for shorter so let me remove this comma as well so every single field is required and when you set data you will need to provide every single one otherwise it's going to give you a validation error which is a good thing that's why Mongoose is great okay now that we have a database setup or schema setup we could go and concentrate on the movie controller to start with the first one as we call the list movies let me come into first of all so we know what it does and to do this I'm just going to put a comment in here so let me do this and the first one is going to be the route so we're going to be using api slash movies for this one and this is going to be sorry this is using a get method so I can do get and what I want to do is get all movies like so okay now that we know what this now that we know what this writer does one thing that I forgot to mention is that when we created the database and we require there in here you could have checked whether everything is working in the command line so if you saved and if you set connected that's good but if something was wrong it'll probably just give you an error but this means that we actually have a connection to our database and we can start using it okay now let's concentrate on this right here which is going to get all movies but before we do that we don't actually have any movies to get so there are a couple of ways of inserting data into MongoDB you can't do it through the compass application or we can do it through the atlas in here we can do we can go brass collections we could potentially create a collection manually in here create database manually here and a collection and insert the data just like here insert document that's a database that I had from a previous tutorial but what I'm going to do instead is I'm going to create a super quick function that is going to insert some data for us and the reason I'm going to do that is because I've already prepared some data which if you want to follow along you can copy and paste so I've copied some movie titles from IMDB and to insert some data I'm just going to create a function here and then we can remove it make sure that you remove this function as you don't want to insert movies all the time so what I'm going to do is async and this is going to be a function and let's call it insert movies and inside here we need to wrap everything into a try catch try catch and inside the try we can do a wait and we can use the movie model that we created so wait movie dot insert many and inside here we set an object alike so and I'm not going to do anything with the error what you can do is just console.log and I'm just going to log the error like so but this is what's important now here and what I'm going to do is insert an array of database movies and let me first show you how that looks like so if I grab one first of all and our object is basically following the model that we created in here so movies as you can see we have name description category thumbnail so that's what it does name description category in category we can have different categories and so on this is what we're going to do and I'm going to list a few more I've already made the list so I'm going to copy and paste it and I'm actually going to comment it for you so you can have it as well if you want wish to copy it so I'm going to paste it here as you can see we have a couple of movies that's all good and the last thing that we need to do is run this function so if I put it here at the bottom so insert movies we run the function if we save this hopefully and I assume that I don't even have to refresh the page anything like that if we go back to MongoDB and refresh okay as you can see we have the movies database automatically created and this movies database has a collection of movies and it has 10 documents that we just inserted as you can see and the good thing about MongoDB is that it adds a unique identifier for every single movie which can be quite helpful and we'll probably use later on and that's pretty much here as you can see the erase is working here so we have drama romance and so on okay let's have a look at how we can actually get this data in adjacent format I'm going to remove this actually and go back to the project and I'm going to remove this because I don't want to be inserting any more movies but I'm going to comment it for you just in case you want it so this is going to be in the project and from now on I'm going to be using postman because we're only going to be dealing with JSON data and this is going to make it so much easier to do the requests so let me show you one of the reasons that I like using postman and there are many alternatives out there in fact you can do it in visual studio code as well so one of the reasons that I like postman is because if you have an account all your collections will sync if you go to if you go on your laptop and you make some changes and then if you go on your pc they will sync which is great and that's why I really like using it and I have an account but as I said there are other good alternatives out there but this one I'm going to be using okay so let me explain so first of all I've created a new collection here which you can create by by clicking the new button here and I've just have a few different requests so we're going to be using today get post patch and delete when I click on get who have done is basically I've created a new tab I've select get in here and I've put the URL which is local host with a point of 5000 and then slash api slash movies so this is what I've done in here and one thing that and that's pretty much it for the get as we go along I'll explain the other ones as well so for the get all we want to do is send the request and get something back at the moment I'm not sure whether this is running oh I must have removed the send rest send and then this is why it was breaking I think okay so if I send one more we get hello world just like you would in the browser but now I want to actually get the data from the database to do this it's actually fairly simple to do and all we can do is wrap everything into a try catch and press enter and inside here we're gonna do our first query which is find so we can do const and you can call this data movies whatever so I'm gonna call it movies so we know what we're getting back and this is gonna be equals await then movie we're gonna be using the movie model schema and then and then dot find and then what we want to find is everything in order to find everything you just put curly brackets and that's it this is where you usually add your filters which we're going to explore in a second as well so that's how you find everything and then if you want to chain more stuff like limit you can do so if you wish to and I'm going to show you that in a minute as well but that's all we need for now all we need to do is get all the movies and now to display them adjacent what I can do is rest dot jason and then inside here I can just pass the movies like so and that's it for the error what we can do is rest dot status and we can just put the status to be 400 and then dot jason and then inside here we can just put a message something like message and then error let's change this as well just to be like so I think that's better and we're grabbing basically the error from here and that's it so if I was to save this add you up a little bit go back to postman and send one more time as you can see I'm getting all the movies in here and they are in a nice format we have the IDs the name description and so on so that's brilliant now let me show you how we can do a few photos now to do that I want to use parameters just like most of the APIs out there for example you might want to limit the results that are coming back and to do this you might want to put a parameter of so question mark and you can do limit and let's say equals to and as you can see this is in here as well whichever is easy for you and you can stack a few more if you wish and we will do as well and let's say if I set limit to I want only two results to appear at the moment if I take this out and let me zoom out a little bit at the moment we have a lot of results as you can see and if I send this it doesn't do anything we're just getting all the results and let me now show you how we can grab this parameter and use it we go back to the project in order to grab the parameter we can use rest.query so if I was to do console.log if I was to do rec.query then if I save this and if I send this again one more time send and if I go to the command line and did I restart save send let me restart let me send one more time okay we go back and as you can see it says I listening on port 5000 connected and limit two so now we can use this to do a simple query or to add a simple filter to do this we can actually change the limit to this here move we find so what I can do is dot limit and I can pass the limit from here so for example I could do rec.query and then limit now there is a problem with this and this probably won't work and this is because the limit comes as a string but we need it as number so let me show you if I was to save this and send I think it's just breaking I mean it should give us an error I don't know why no one is being a little bit slow for me but okay as you can see nothing is actually happening it's not working and this is because this is treated as a string so what I'm gonna have to do is convert this into an integer so what we can do we can actually set a default one so I can do let and inside here we can destructure this object by doing limit and then this is gonna be equals rec.query like so so we're destructuring the object and I can actually give this a default value of let's say 10 so if we don't put any limit the default value can be 10 so I can put this in here and the next thing that we need to do is if we do get a limit we want to convert it into an integer and to do this we can do let's say const and oh because I called the limit here we might have to call it something else now so let's see let's see maybe limit records limit records and this is gonna be equals pass int and inside here we put int now instead of having the limit because I renamed it I'm gonna have to put it in here so let's see what happens now so if I go back and if I send we should be getting only two results as you can see only two results appear but if I was to remove this the limit send we're getting all the results which is the default number that we set here now there is another way of which you can do this you could potentially do it by doing if statements so let's say if limit does not exist then maybe you can set limit to be equals 10 and that would be pretty much the same but maybe you can do more complicated stuff in here if you wish to so I'm gonna remove this and just keep it simple so this is gonna be equals 10 and the next thing that I want to do is add a skip and the skip is going to basically make it so we can have different pages for the results so maybe we can have page one with results 10 and then we can have page two with results 10 and there will be different this is great for usually for pagination so let me show you how this is gonna work so we have limit and now I can add page as a default it's gonna be set to one now to use the page we're gonna have to do some trickery in here so what we can do is const and I'm gonna call this skip and this is gonna be equals page minus one and that would be equals time the limit here which is at the moment set to 10 as default all right let's have a look at how we can change this now so I can use the skip and after the limit here we can do dot skip and we can pass the number from here so that should be good enough and look at what happens now if I go back we send we get 10 records as default but if I want to limit it to two equals two we get two results but if I want to do pagination I could potentially do ampersand to create another parameter and this is gonna be page and let's equals this to one which is the default so nothing should change but Kazin Royale is here and Titanic is here but if I change it to two you should see that the results are now changing and this should be very helpful for pagination okay now that we've done this I want to take this slightly further and do custom queries like for example what if I wanted to search for specific category or maybe I want to search for the entire database and I want to find out something like James Bond now to do filters is actually fairly easy but I'm going to do something slightly more complicated now for the filters just to explain it what you have to do is let's say if you want to filter through categories what you have to do is grab another parameter let's say category and when we grab this this is going to be a string which is absolutely fine so I can use this and I can say okay inside the database where we have categories category sorry we want to pass the category that we're getting from the breakdog query and let's see what happens so if I was to go back and say category action let's say that and if I put question mark category equals action and if I search you will see that we are only getting action action action and so on so all the movies that we have in here action and let's say we want adventure only so I can just put adventure and all the adventure movies will come up which is kind of great so this is how you can do that but I actually want to take this even further and I want to be able to search through the entire database to do this it's a little bit more complicated we're gonna have to build a query for everything because we have the categories here and we're gonna have a custom query and so on so in order to be able to do that we're gonna have to index everything in our schema so let me show you how we can do that undefined let's remove all this first of all save it let's go back to a schema which is in the models movie and inside here you can do all sorts of stuff you can make it specific to maybe the name and the description but I want to go through everything so what I can do here is I can use the movie schema dot index and then what I can do is a little bit crazy but what I can do is in double quotes I can do dollar sign ashtricks column and in single quotes we need to put text like so and that would basically index everything in our database now let me show you how we can use this it's a little bit tricky but if I save this first of all and if I go back to find here let me just first show you how we can do it and then I'm gonna build a few different queries so in find if you want to find everything you do that no problem but if you want to search in the whole database what we have to do is in curly brackets we need to put dollar sign text quote and then another curly bracket set in here and then inside here we need to put search and then the parameter that we want to search for so for example I can search for James hopefully if we go back and send we should be getting James Bond which is great and I can do the same for the other ones so this search is for the whole database and maybe I can just do action and that should also do it action send and that searches through the entire database and show all the action movies and let me show you maybe if we put joker one more time and send this we have the dark night which is absolutely fine so it looks through the description and it finds the phrase now to be able to do this from the URL what I'm gonna do is I'm gonna use a query parameter just queue queue for query so I'm just gonna leave it as two and then whatever we set here so it could be I don't know it could be joker obviously this is not gonna work now but let's build it up and make it work if we go back to build all queries I'm gonna first of all gonna grab this because this is hard coded at the moment so that won't work for us so let me grab this and let's start building all query now I'm gonna create an object that is basically going to contain the whole query so let's do let query this is gonna be our object and then inside here we just open and close curly bracket if we want to start building the query what we can do for example for the category which we've already created so that query is gonna be inserted into here into find so what I can do for the category because we've already created that I can do if we have category they want to insert this category inside the query so I can do query the category and this will be equals the category that we get that's it so here is our query here and hopefully if we go in fact you know what we can we can console log this as well now let's do console log and I can just console log the query just so we can see what we're getting so if we send this and nothing is happening because we don't have a category action send and we're getting action which is cool and if I go to the console you can see that we have an empty object festival and then we have the category set to action which is good let's build the other one now the query which we can build in here so you can name this whatever you like by the way I'm just gonna put it ask you short for query so to do this I'm gonna do it in here sorry in here and I can do if the same thing basically if query we can do we can do if query let's say query and by the way this is a shorthanded if statement but we can do we can do it this way as well so if query we can do equals and then we can paste the text that we had earlier so that would be the text that queries everything in our database and that would be the dollar sign text column and then dollar sign search column and then we pass the query like so close this should be good to go and now I'm gonna remove this by the way and now that's inserted into the query as well and if we go back to postman and send everything is working so let's search for scream maybe something in here so let's go for example in the matrix and let's say something like hacker I'm gonna use this query so we're gonna do question mark queue for query equals hacker and if I do send you'll see that the only result we're getting is the matrix which is working you can definitely stack up all the parameters together and they should work as well so you can say maybe I want to search for everything crime and I want to limit the result with two so we're only gonna get two crime movies or and now I can even add the page and that can be equals two but I don't know oh we do have more movies okay so that was lucky we do have more movies if I put one you will see that they change and so on you can definitely add more parameters like this but one thing that I noticed just now is that if I was to move the category at the bottom so maybe that needs to be added last in the query that seems to work a little bit better with filtering the category so for example I'm I'm searching for everything crime where limit is two pages one and category is biography so this is super specific but as you can see it seems to be working which is great and now we spend too much time on this let's move on to the next one which should be quite quick to do where we have post patch and delete let's go back to a code and the first thing that we need to do is create or new route and to do this we can go back to routes and inside here we can start creating them let's copy this by doing all shifting down and I'm going to do right dot this time it's going to be post and I'm going to post on exactly the same page so API movies movie controller and this one let's call it insert single movie this is far too specific but I think it's going to work well and now we want to create this controller insert single movie we can go under movie controller.js and create in here so I'm going to copy all this and paste it and what I'm going to do that stays the same that goes to post and we're posting single movie and this should be replaced so export insert single movie and then we do the magic in here now for inserting movies it's actually fairly simple we need to be able to grab some data and insert it into a database so let me show you how we can do that first of all obviously we're going to be using the movie schema so this one here and what we can do is create a new movie object so const let's say new movie and this is going to be equals new movie and then inside here we want to grab some details so for example we need to grab the details just like in our schema which is here name description category and thumbnail they are all required so that's important and let's have a look how we can grab them so to do that we can do name and then we can do rec dot body and then name I'm going to show you how we can send the minute in second as well but this is how we're going to grab them and then I can duplicate this three more times and this one is going to be changed to description so let's put description this one is going to be changed to category let's change it to category and then this is going to be thumbnail thumbnail and thumbnail and we don't actually sorry we don't need that we need commas in here instead okay that's looking good and now to insert this all we can do is wrap everything into a try catch again and then I can just do await and then new movie dot save that's it that's going to save the movie and again I'm going to do this here so rest dot status 400 dot jason message error and I need to change this to be shortened to error you can call it whatever you like and that's it so if we go back to postman all you have to do is create a new tab make sure that the request is set to post make sure that your euro is like a house 500 5000 api movies and then click on body click on raw and make sure that this is select as jason now inside here as you can see I've already have one already written what I have is a simple object here with the name description category and thumbnail and if I was to send this now something isn't working and this is because oh and I think this could be because we are not doing anything with this right so we need to rest dot jason or we need to do something um we need to rest dot jason this and we can just display the result so let's do that make sure okay this is all looking good let me try one more time send and as you can see we have the record here which we inserted and this should give us a unique identifier as you can see which is pretty cool so now if if I was to jump to the database super quickly if you go to the collections and scroll down to the bottom you will see the new record that we just created I'll zoom in a little bit but yeah you'll see that we created a new record which is good and let me now show you how we can update this super quickly so to do this we can go back to route create a new route this is going to be writer.patch and this is going to go to api movies but this time we want to specify which movie we want to update in order to do that I'm going to use the id and I'm going to show you how we can grab this id from the arrow in a second and update it so instead of insert let's put it as update updating a movie and save this and let's create the controller so inside here I'm going to copy all of this so we can do api movies and we need the id in this case and this is going to be patch single movie or update single movie and then inside here we need to update this insert to update single movie and in order to grab parameters we can do let's store in parameter id param id and this is going to be equals rec.params.id this is how we're grabbing the id from the URL the one that we put here and the one that we're going to send with postman in a second so if we have this then we also want to be able to update something like the name so I can do let name and this could be equals rec.body and then name so we want to be able to grab the new name and update it now we can wrap everything into try catch like so and inside here we can say maybe const update movie equals await and then we use the movie dot update one this time and then inside here we want to say which movie we want to update so we can select it by the id so to do that we put id and then we actually pass the id from the URL with the parameter id and then we want to do and then we want to update the name with the new name so we can do the name this is in the database and we want to pass the actual name that we're going to update and last but not least we're missing a curly bracket here okay remove that so curly bracket here so we have curly bracket here curly bracket here curly bracket here okay this is all good all the error I'm going to do exactly the same as the rest and also we need to respond with the results so rest dot jason and then we do update movie like so we could put this as data to be honest but that should do the job so now if I go back to postman and if I go to patch which is my update one my update collection what you have to do is put the URL put the id that you want to update and then pass a new name so in this case I have this record I want to update I'm going to grab the object the id I want to update this so I'm going to put it in here on the URL and I have the name set up to the dark knight and I'm going to send as you can see the result goes acknowledge trip modifier field count one and so on which is good so technically if I was to refresh MongoDB and go back to the bottom you'll see that we've updated the name and we can do exactly the same thing with the description category thumbnail and so on now let me show you the last thing which is how to delete a movie by ID so we're going to try to delete this by ID very similar to what we've just done with the parameter I'm going to copy all this I'm actually going to copy everything and see whether we can modify so this is going to be movie API API movies ID delete single movie and then we grab the parameter but we need to change the query here a little bit this needs to be changed to delete one instead and we just need to put the ID that we want to delete so we don't need the name and I think oh instead of updated maybe we can just put movie or data data back so it doesn't matter save it and now if I was to grab this ID one more time copy and if I was to paste in here so we have body raw sorry this is not the one I need to go to delete okay so we have request as the lead we have the same URL I just need to change the ID in here and that's it so I don't need to do anything else actually so if I send this it looks like we we cannot get the lead and this is because we probably didn't create the delete oh okay we need to update this festival to delete I was russian and we need to put this into a route so this needs to go to router dot delete and then API movies ID and then delete single movie like so how we should be good to go save save this and let's go back hopefully everything is running okay let's recent deleted count one and now this should be gone this result should be gone if I refresh and if I go back to the bottom you will see that the one that we just deleted is gone which is great all right one more thing before we go if we go back to the list movies what I wanted to show you is that you can actually also add the page and the limit inside here as well so to make it a little bit more professional in in an object like so so curly brackets everywhere and then we can do page we can grab the page numbers which is here as default is one then we can do comma and then we can do the limit and then we can grab the limit here limit record but that and then we can pass the movies that might make it look a little bit better if I was to go to get send and you will see that we're getting page one limit 10 movies so that's a little bit better and if I was to say page equals two you'll see that we're getting page two but I don't I probably don't have enough movies maybe and then let's say limit of three okay page one limit three if I say limit three and page two that should work because I have enough movies for this but as you can see these changes which is great and the last thing that I wanted to show you is that if you want to use this in a real-life application I've just made a very simple fetch request here is it's another file I'm not gonna explain this but let me run it super quickly so here is the document and I'm basically fetching the API that we just made they come up in here and everything seems to be working and I have just a few images in this folder as well and what I wanted to show you is that if you were to go to this html and if I was to change the URL to uh the limit to two save this go back you will see that we're getting casino royale and Titanic and you can work with this a little bit more so if I was to do Q for query and put matrix equals and we just get the matrix like so and that's gonna be the end of this tutorial I hope that you enjoyed it I hope that you learned something new don't forget to smash the like consider subscribing to my channel and comment below