Create Scehma in mongo DB || All about schema || Create new user in mongo DB using node JS

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys welcome back to the debug arena in the previous video we have connected our node.js server to the mongodb and now in this video we are going to create a schema and also we are going to insert some data into our mongodb so let's get started so the first thing is what is schema you can say in simple term that the schema is a structure for data or you can say blueprint of a data it means that how your data should look like now we are going to create a schema for the user detail so what are the variables that users would contain like user id username user password so the all this thing combine make a schema so let's make one schema i will make a new file user details dot js so the first thing is you have to import mongoose into import you have to use require mongoose okay now create a variable like i will say user details schema and i will use a function of a mongoose known as schema so now inside here we are going to define all the fields that are there for a user so like there will be a username which is of type string and for now there will be an email which is also of type string and there will be a phone number you can say that might be of number but we are giving it for a string so we are only creating this three as of now so this is known as a schema like you are defining the properties of a user so after this you have to store it in a collection so you have to write this here collection and you can give it any name i will give it user info now in last we have to create a model mongoose dot model and inside that password collect your collection name user info and pass your schema so now this schema will be developed in this collection in your mongodb server just save it so now we have successfully created the schema now let's import it now now let's import it in our app.js so i will import it using require the file name user details now notice here now i am creating a variable name user and accessing that model yes i am going to access this model so whatever's information has been stored into this model i can get it inside this variable so you have to just write mongoose dot [Music] model inside that write the model name so our model name is info so currently we will get nothing inside user as uh there is no data now let's create and register api through which we can register any user so i will be using post give the name of the api as register [Music] okay now use try and catch block okay before that to register user we will require some data from the user so we can access that data by request.body i had already explained all this in the previous video so i am going to take name [Music] email and mobile number from user that is request dot body okay sorry request dot body so this step is done now what we have to do inside try and click cache block write await i am accessing user and i am creating a new user so you have to just write create now here you have to notice that here i have to create a user with a name you name this union refers to the same name that we have given the schema otherwise it will not work and you name have a value of this name that we are receiving from request.body so keep this in mind so you name and email i am writing email once only because email here that we are receiving from request.body and the email here that is in the schema both are same so we can write in this way also and the other one is mobile number so phone number phone number will get the value of mobile number okay so i just change the name of the variable so that you can understand these now it will create the user now just send a response status is equals to ok otherwise response dot send status is equals to error now let's save this open the postman okay write a register api name don't forget to add header i had already explained all this thing in the previous video now here we will pass name we will pass all this variable with the same name otherwise it will not work name others will pass email then i will pass mobile number okay now click on send and you can see here that we are receiving status is equal to ok now go to your mongodb click on browse collection make sure you are in the same project and now you can see here that our model has been created our collection has been created which is user info and our data has been entered we had registered a new user so in this way you can easily create a new user in mongodb through node.js so now let me quickly explain you what has happened here so i had created an schema name as user detail schema and give it this three value and store it in the user info and this will create a model now i had imported it in app.js and accessed this model then i had created a registered api in which i am receiving these three value from users you are from user and then creating the new user so in this way you can easily create a new user in mongodb so i hope you had clearly understand what is schema why to create an schema how to import it and how to create a new user in mongodb through node.js now in the next video i will be creating a full-fledged login functionality but we are not going to use postman we are going to test our node.js api in react native application by using fetch function so that's it for this video if you found this video helpful then please like share and subscribe thank you [Music]
Info
Channel: The Debug Arena
Views: 10,892
Rating: undefined out of 5
Keywords: how to create schema in node js, how to create schema in mongo db node js, how to create schema in mongoose node js, mongo db, mongoose, node js, create user in mongo db using nod js, how to create register api in node js, learn to create new user in node js, connecting node js with frontend, learn mern stack, create api in node js, learn mongo db
Id: 2B5Hx4JQXns
Channel Id: undefined
Length: 8min 40sec (520 seconds)
Published: Mon Jul 04 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.