Connect MongoDB with Node.js using mongoose | MongoDB + Express.js

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone welcome back to another video so in this video I'm going to show you how you can connect the mongod database with the express application so for that let me just simply uh open the mongodb compass so if you haven't installed mongodb and mongodb Compass so I have got separate video for that as well so you can just check out on the description so once you install mongodb and mongodb Compass just simply open and just click and connect and simply create the database so you can just click here on the plus sign and create the database something like uh DV connect and give the collection name something like users and click on create database so this will create the database DV connect and the collection users so now let me just simply go into the vs code and start writing the code here so for that so just click on open so you can just simply create one folder inside the desktop or whever you want so let me just uh say something like DB Connect app and just click and enter and click on open so this will open this uh DV connect here so let me just open the terminal and simply type npm in it to initialize the application so you can just give the package name as default and click enter so you can just give the description something like connect mongod DB with Express and just click enter and just click enter so you can just press enter and you can just simply uh write the auor name something like code with or if you just click enter so this will create this packet. G here so inside here so we will be installing uh different npm packages as well so before that so let me just create the uh index.js file here and now let me just go into the browser and here you can just simply uh type npm so inside here so we will be uh installing the first Express and here you can see so this is the uh command for installing the express you can just simply copy and you can just simply come here and clear the terminal and paste it inside here and just click enter so this will install the package inside this dependencies here so once you install the express so we just have to install the mongus as well so you can just similarly Source mongus here as well so you will be able to see the instruction here or you can just simply type from here as well so you can just type npm install mongus so mongus is basically a object modeling tool so just install the mongus similarly so we will also install theb SOB is basically for loading the environment variable fromb file so just type npm install dob and just click enter so this will install theb as well similarly we will also install the de dependency called nodemon so which will help us to uh restart the application when there's some file changes it so just go into this node modon here so if you just scroll down so you can see so you can just copy this line from here and simply paste here so this will install it as a Dev dependency so once you do that so if you just scroll up here so you can see entry point index.js so just below there you can just type type here and just give module and inside this script so just type start and you just need to clear this and just type node Monon index.js and save this so once this is done so we will now create theb file inside this folder so just type dob so this is basically for loading the environment so just go inside here and just type Port so this is basically a port where our application will run and also give the mongoor URL and here inside here so we will put the URL so if I just go into this Compass here so top you can see Local Host 27017 SL DV connect so we will just put the same link there so just type Local Host and just type 27017 SL and the database name is DB connect which we have created at the beginning of the video so this is DB connect and just before this Local Host so we just need to type mongod DV column sl/ so once you put the mongod DV URL so just go into the index.js file here and just start importing the npm packages so just import Express from Express and similarly just import mongus from mongus and similarly we just have to import the ENB uh which is basically for loading the environment variable fromb file so just type import dob fromb so now just create an Express app instance so just type const app just give equals Express and just below there so we just need to load the environment variable so for that just typeb do config so now we can get the port and mongodb URL just by typing const Port just create the variable and just type process dob do Port which is the name we have given and by default we can just give the uh default Port so if any port which is using 8,000 you can just give the uh other alternative Port as well and also so just type const URL for that we can just type process. ENB do uh and we can see URL copy this URL and paste inside here so this is how we can get the port and mongodb URL from the EnV file so now let's connect our mongodb with Express using mongus so just type mongus do connect and just give the URL from here and just type dot then and so if the connection is successful so we can just give the message something like console.log and just type something like database is connected successfully and also let's start the express server on the specified for and we can also log the running starts on the console so just type ab. l so just type ab. L and just type the port and just give the message something like console.log and just type something like server is running on Port and you can just uh print the port number by just typing dollar and just type port and so this will uh print this port as well so similarly if there is any erer you can just scatch the erer by typing erer and just write the console do log eror so once this is done so I think uh it will be enough for us to connect with the database so you can just go here and clear the terminal and just type npm start and click enter so you can see the database is connected successfully and server is running on Port 8,000 so once the database is connected successfully and the application is running on a specified for so let's write a simple program to uh get the users data from the database so for that let me just again go to the uh Compass here mongodb compass and you can just simply click on add data and click on insert document so just below this ID you can just give something like a name and you can just give the name something like Jun and just give the A and you can just give the AE something like 12 and you can just simply click and insert so now we will write the code to get the data from the database so for that again go to the vs code and here let me just create a user schema so for that just type const user schema and just type new mongus do schema and inside here just give the name and the type is string and you can just give the age and the age is number so let me just go below here and similarly let me create a model named user model uh for the users collection so for that just type const user model and just type something like mongus do model and the collection is users which we have given on the mongodb compass here so you can see uh we have given the collection name users so let me just write the users here and just type comma here and just type the user schema from here just type user schema and let me now just create a request to display the data from database so just type app.get and let me just give the path something like G users and just type asynchronous function and just write request and response and in response so we will be displaying the Jon data so just type cons user data just type await and just type user model. find method just type find so this will get all the users and we will uh display it in G so you can just simply type respond. J and just type user data and let me just save this file and now if I just go into the browser and let me just type something like Local Host 8,000 since we are running our application on 8,000 port and just type slash and we have given the uh path get users so just simply type get users and just click enter so this is showing the name Jone and S2 so this is showing the raw data so let me just install one extension called Json formatter so just search for Json formatter and just go into this uh Chrome web store so you can just simply uh add to your browser so once this G formatted is added so if I just uh click enter here so you can see so this is coming in this format so let me just simply again try to add one more data so you can just simply click on add data insert document and let me just write name you can just give the name something like Tom and give the age something like uh 20 and if I just click on insert and if I again go to the browser here and reload this so you can see the data is added here so this is how you can just connect the mongodb database with the express application thank you so much for watching if you like the video be sure to like share and subscribe I will see you in the next one
Info
Channel: Code With Arjun
Views: 25,420
Rating: undefined out of 5
Keywords: connect mongodb with node.js, connect mongodb with express.js, connect mongodb using javascript, mongodb, mongoose, mongodb database, mongodb using express, mongodb nosql database, mongodb for beginners, mongodb using localhost, mongodb using mongodb compass, mongodb database connection tutorial, mongodb url connection tutorial, mongodb tutorial for beginners, mongoDB, mongoDB tutorial for beginners, mongodb and nodejs, mongodb and express, mongodb code with arjun, code with arjun
Id: 30p9QfybWZg
Channel Id: undefined
Length: 10min 43sec (643 seconds)
Published: Thu Feb 08 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.