How to Connect Node.JS with MongoDB using Mongoose | Connect to a MongoDB Database Using Node.js

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone in this video we will connect our node.js app with the mongodb okay in the previous video we installed mongodb locally we installed the mongodb compass so in this video we will just connect this mongodb Compass I'm going to be database with our node.js app locally okay so let's first of all create an app to create a node.js app so name it server and inside this let's come to the command line CD server inside this we will install um first of all npm in it hyphen y to initialize it and it will create a package.json file inside it and let's install now npm install Express mongoose yeah we will install these two packages expresses the framework up node.js we through it we will create rest apis yeah they will be installed right now the idea is installed successfully so now let's create one another file by the name of index.js where we will write our code okay so let's first of all import the express const Express is equal to require Express and also the one goes sorry mongos is equal to require mongos let's scans create an app is equal to express called the express method and after that we will just use Mongoose dot connect and we will connect to the local house let's come to the compost and copy this URL located connection string copy connection string just paste that here and slash and after that many of the database we have a database in our compass that is by the name of craft I will name it grad now let's run our server up that lesson assign a port number for example three double zero one and let's create a function callback function and console.log server is running yeah so now let's create an API to access the data from this database okay to access data from this database this regard so for that just come here and write up dot git get users and create a callback function we will get a request and response and inside that I will just use the um uh just to get the three cards so to get the record I will create a model before so we can hit that in a separate file but I will create that here const user schema is equal to start schema and inside it let's define the fields name string and Edge number and after that let's create cast user model is equal to this is new mongoose and here Mongoose start model here first I will Define The Collection that a table name that is users with us and the second one is the user schema that we have created and now let's use the user model user model dot find yeah let's say yeah it's okay let's just result the adjacent we will return them I hope it will work so let's run the server go to index.js server is running so let's run this localhost 301 and get users uh question position so we got an error so let's come here so let's see assign this is the query which is optional and here I will write Dean will create a function and we will get the users here let's result.json return the dot cage there was any error and just remove this one yeah it is okay so let's save it and let's come here and close it index.js and the server is running let's come here and we appreciate see we got the data okay name your sub page 23 so it was all about the connection half node.js with the mongodb using mongos so I hope this video was helpful for you if you like this video don't forget to subscribe our YouTube channel and thanks for watching
Info
Channel: Code With Yousaf
Views: 52,023
Rating: undefined out of 5
Keywords: connect mongoose to mongodb atlas, how to connect mongodb database using node js, connect mongodb with node js, how to connect database with node.js, mongoose, connect mongoose with node js, connect mongodb atlas with node js using mongoose, connect mongodb to react app, how to connect mongodb with nodejs, how to connect mongodb with node js, connect mongodb with react js, userform data to mongodb using mongoose, connect mongodb with express js
Id: ACUXjXtG8J4
Channel Id: undefined
Length: 5min 50sec (350 seconds)
Published: Sun Jun 04 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.