MongoDB (Atlas) connection using Node

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
I do welcome to my youtube channel gain in this video I'm gonna create MongoDB connection with using node chest for this I had played a created a blank folder just to open your terminal and write npm space in it space - why that it'll create a package store JSON file so that we have extra JSON file I need to install for the dependency so use NPM I express and Mongoose hit enter till oh look this dependency store just move to official website of gamma DB and after sign in you will see the first creep and there you have to create a new project let's create a new project and for this project you can give any name according to you and just I'm using test fraction for MongoDB so I'm using like moomoomath note as we are using node suggest to create project after that you'll see screen new at Togo's build colostrum and you can select the option on your screen like cloud provider and raizeen put you whatever you want to select at the bottom screen you will see three plus true there you have to click on create cluster this will provide you 512 mb space so it will take some time for creating your cluster so take some time like seven to ten minutes till I had came back to whom my coding part so they don't go to create a folder and I'm gonna give it name of TV that will DB connection hold our connection of database and I'm going to create another folder for route also I'm going to create a file server dot GS that is the entry point of our application so user wants to express is equal to require Express then after coaster app is equal to express and use parentheses then Const Ford I'm going to use our portal 3000 then after like process totally and V dot port like it will check to your environment whether it's for taste-free or else it will use 3000 then after choose so we you know listening this particular app on this port so app totally support and I'm gonna provide it a callback this provide us console dot log and it will return my message like server started so when we gonna run it it will provide us this message in comes also will able to know like our application is working fine and the outer role is to me so just start it using node space server is so in consoled you will see a message service started in T in D before I'm going to create a new file so I'm going to give it a name of connection without CSS Mongoose is equal to require bongos also our comes to URI that will hold the string that meet a connection with our database so let's grab this from our cluster just click on cluster and there you will see the connect button just click on it then after I'm gonna create our user 0 what sir username TP user and just need the password I've seen as the username tip user like I'm going to use it only for tests or you can give a username and password according to you now you have to choose the connection method I use just close this pop-up and connect your application there you will find your URI string so just copy this and all we need we have to paste it here and just change the username to your usually whatever you had provided and similarly change the password as well to be you sorted fine so I'm going to create a variable cost on connect TB is equal to this will be on arrow function inside it body I'm going to use Norwoods dot to correct and inside it I'm gonna pass your eye so it will simply connect to your database and this will be an asynchronous method so I'm going to use a sink and a weight before the Mongoose third connect and just exported this module so module dot export is equal to connect GB now use this in our server dot JS file so there we need to import it to first select a Const connect TV is equal to require and do you have to give the bottle of your file where you had to create the connection so it will be TV's last connection and then after I had to use connected EB method so just use this here and press m equal up here that's fine also in connection the gonna print a message in console dot log like our database has been connected like whenever we run the program it will simply show the message whether our TB has been connected or not just true run your terminal to wrap it up so not space server it's showing some error also it's so initial warning related to deprecated just copy this and you had to use it inside start connect and after you re just separate it coma and paste it you and use another one you use new URL parser is true as well so save this also I'm gonna use a node one as well so NPM I - t node 1 as we have to starts over again and again so by using node mode we do not need to start house over again Enter key so in script also I'm going to use a server and what it will be description like northome space server to server tortillas so when we cannot run npm run server its to will simply run command node one space over 2gh for us so npm run server so server has been started and our DB also connected as well so that's fine we had made connection with our database also our the few tips for you like whenever you're going to create the connection just go back yeah you had to are white a list your system IP a time to time and in the IP you had to use your public IP you can simply search on Google like my IP and there you can simply paste your IP and simply use vitaly's to your work so inside TP folder I'm going to use user tour GS anyways I'm going to create model for our database or you can simply say is FEMA as much use schema for this I'm going to use long ghost is equal to require mongoose also Const user is equal to the Mongoose dot schema and since so we are gonna create the object of this so all of I need I had to use a new keyword before Mongoose dot is FEMA yeah there we go and inside it I'm gonna use whatever parameter I need in my table so you can use a first name and it will hold the type of string also I'm going to use a last name as well the of similar type string so just paste here and change to the last name in our module dot export is equal to user and which holds the reference of Mongoose dot model and which we had created user model so Mongoose touch model inside it I'm going to pass the user comma and Hughes pubes are here so that's fine we had successfully created the model for you so I'm gonna create one more folder for API and inside API I'm gonna use user dot GS so first Express is equal to require Express with this file I'm gonna make interaction without database like when we hit this API you from our postman it will simply create the scheme your user model with a first name and last name in your database so use mumbles as well also your scones to user and give the path of for the file this is inside of your TV TV slash user also gonna use coaster router is equal to Express daughter route it's fine I'm gonna use this route here so router post as you know make a post request for this used X less and I'm using the function here which hold request and response so like Const then I'm gonna use first name and last name this is equal to request dot body like wait I'm gonna pass first name and last name in the body technical hold inside the request and it's simply a sign that the value of this to your first name and last name so that a user is equal to and blank object and user taught first name inside it I'm gonna assign the value of first name which came through the request also use of total last name is equal to lastname so both of these value came through the request and it's just simply assigned to the use of s T object select user model is equal to a new user dot a user is hold the reference so for that particular file so inside it I had passed the reference of user now the user model thought sale it will say to in our database and I'm gonna use wait for this and make this function I think so I'm going to use s including a skull with this function also mmm I'm gonna send back our response like emanated CEO to our database so response totes and G shown the user mode so what about our save inside our database it's simply send it as a response so now use this app taught use and then I'm going to do a back slash API slash user model and it separate with I'm giving the path of it so require inside our API and inside it user the user file so just save this I'm getting some error it will be expressed or it will be router by mistake I had her write it route interval P router we capital R or ute or dead fine also I had to export this module so module dot export is equal to the route let's why not see all of this all of things is working fine as opiate this API user model and just move to the postman and they use localhost 3000 whatever your poor to using after that separated backslash and use usual model inside OD I'm gonna pass JSON data so use curly brace inside it I'm gonna pass first name and use the value of this set array as I'm going to print the name of my channel Saturday developer so first name is Saturday and second name will be a developer and so for a last name so this will be developed just send a request it's own some error because this will be a post request send this again and it will take some time can just cancel this request also like I'm gonna return the JSON data so - one more thing I use here app to use Express to JSON as we are returning the JSON as it responds so Express dot JSON and inside it I'm going to use extended to defaults the tryna save all of this now to post and again and Sandy requests so there you can see are we had successfully send the request and in more to our database and in collection you'll see we had user collection and inside it we had first name and last name I do created by MongoDB by default just make another request for like test user and gives evil or name 3d last name test user yeah there you can see output and inside our collection inside user we will have a two results so one for Saturday develop our second for test futile so we had successfully made connection with our MongoDB and created our model using node.js so if you enjoyed the video then hit on Mike button also if you are new then subscribe my channel and we will find this host ode in the description
Info
Channel: Saturday Developer
Views: 57,737
Rating: undefined out of 5
Keywords: mongodb download, mongodb documentation, mongodb tutorial, mongodb example, mongodb compass, mongodb atlas, node js documentation, node js examples, node js download, node, node js tutorial, saturday developer
Id: Qn0SOL8vK8w
Channel Id: undefined
Length: 22min 13sec (1333 seconds)
Published: Sat Sep 21 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.