Complete MongoDB Tutorial #15 - MongoDB Drivers

Net Ninja · Beginner ·🔧 Backend Engineering ·4y ago

Key Takeaways

This video tutorial covers the use of MongoDB drivers for communication with MongoDB on a programmatic level, specifically using the Node driver and Express framework for building web applications in Node.js. Tools such as MongoDB, MongoDB Compass, Node.js, Express, npm, VS Code, MongoDB drivers, and MongoDB shell are utilized.

Full Transcript

all right then going so far we've seen mongodb interactions in their purist form without using any specific programming language or environment right like python or node or ruby or php or something else like that and we've only been using the mongodb shell and mongodb compass to interact with our database but a lot of the time you're going to be interacting with mongodb from your application code for example from a node.js application or a python application etc now to do that we'll need to make use of specific mongodb drivers and for each environment or programming language that you might use there's a specific driver for that which gives us specific language bindings so if i'm making a python application i'd use a python driver for mongodb or if i'm making a node application i'd use a node driver for mongodb and these drivers essentially allow us to communicate with mongodb on a programmatic level now you can see a list of drivers available for different programming languages on the mongodb site by going to resources in the top nav and then selecting drivers and then we can just select whatever driver that we need now we'll spend the rest of this series making a node api which communicates with mongodb so we'll be using the node driver to bridge the gap between our node application and mongodb so if you click on that it's going to show you how to install a node driver and how to use it now before we get too far with this i want to point out that for the rest of this series we'll be working with mongodb in a node application so first of all if you want to follow along you'll need node installed on your computer and ideally you'll also have at least a basic grasp of how node applications work and how to make a simple express app if you're completely new to node i would highly highly recommend you check out my node crash course first of all and then come back and i'll leave a link to that course down below the video if you do have a basic understanding of node then let's crack on and set up our application and then install the node mongodb driver all right then so first of all open up a new folder in whatever text editor you're using i'm using vs code but you can use whatever you want and then inside the text editor you need to initialize a new project now remember to do this you have to have node installed so in vs code i can open up an integrated terminal by going to terminal and new terminal if you don't have vs code and you need a separate terminal you can use command prompt or windows terminal whatever you want to use it really doesn't matter just make sure you cd to this directory whatever directory you have open in your editor and then you want to type npm init this is going to initialize a new node project for us and it's going to create a package.json file and that's going to keep track of all of our dependencies so i'm going to enter through all these options just to keep them the default values and then at the end of all this we should see this package.json file right here okay cool so now what we need to do is create a new entry file for our project our api so i'm going to do that new file and call it app.js so this is where most of our code is going to go so we're going to create an express app inside this file and to do that we have to install the npm package express so let's say npm install down here express and then save this to our dependencies so press enter and that's going to install this express package for us if we go to package.json we should see express right here and this is the version i'm using all right cool so then now we can start to flesh out the express app in this file the first thing we need to do is require express so const express is equal to a require and we want to require express all right and then after that we want to initialize the app so i'm going to do a little comment first of all to say init app and also middleware which we'll add later on and then right here to initialize an express app we just have to invoke this function right here so this returns us a function which is stored in this constant we just need to initialize the app by invoking this function so i'm going to store it in a constant called app and set it equal to express and then invoke that function all right so that's all we need to do to create the app essentially now we need to listen for requests and that's going to be on a specific port number now to do that we can say app and then use the method on that called listen so we invoke that then we specify which port number we want to listen to well it's going to be 3000. so that means when we view this in a browser we can go to localhost on our computer port 3000 and then this app right here is going to listen for requests to that port number again if all of this is going over your head then definitely check out my node crash course first of all i go into this in much more depth in that course all right anyway we can also fire a function once we've started listening for requests which i'll do and i'll just do a console log right here and inside we'll say app listening on port 3000 all right let's spell this correctly as well listening cool so now we should see that in the console down here once we start listening for requests all right the last thing i want to do is a little comment to say routes and all of our different route handlers are going to go down here now to begin with we'll set up a get handler so a get request handler that means if i send a request to localhost port 3000 forward slash then we're going to have a get request handler to handle that request all right now in our case it's going to be forward slash box that we want to handle because that's the resource we're ultimately going to be dealing with so what i'm going to do is say app and then use a method called get to handle a get request and that's going to be forward slash books like so and then we fire a callback function when that request comes into this url and we have a request and response object inside that handler function which we automatically get then all we want to do for now is just send a response back to whoever's making the request so to do that we take the response object right here and use a method on that called json to send back a json response and i'm just going to send back a simple message property on an object and that will just say welcome to the api all right so that my friends is pretty much it for now we have now installed express and we've required it right here we've invoked that to start our application and then we've used a method on the app called listen to listen to a specific port number and then down here we have set up our first route handler for get requests to forward slash books so that's localhost port 3000 forward slash books on our computer when a user types that into a browser or sends a request from somewhere else then we're going to handle it right here we'll send a response which is going to be json and this is the json we're sending back pretty simple all right so the next thing we want to do is run this application now to do that i'm going to be using nodemon and that allows us to spin up a development server and it's going to watch our file for changes and when we change the file and save it it's going to restart the server automatically so we don't have to keep restarting it ourselves manually so you'll have to install nodemon to use this and to do that you want to install it globally on your computer so you can say npm install hyphen g to install it globally and then nodemon like that and press enter that's going to install it on your computer and then you can say nodemon and then whatever the name of the file is which is app and then press enter and hopefully this is all going to work now cool so we can see app is listening on port 3000. so if you go to a browser and go to this address localhost and then a colon and then port 3000 and then forward slash box because that's what we set up the handler function for for that get requested forward slash books and then press enter you should get back that json response in the browser which we do awesome we get the message welcome to the api now later on instead of just sending about this json we are going to be communicating with mongodb getting all the books and sending the book jason back instead but for now i just wanted to make sure that this endpoint was working all right then so the next thing we want to do is we want to install the mongodb driver for node so to do that just open up a new terminal i'm going to click this plus icon to do that make sure you're in the correct directory for the project and then just type npm install db that's the package name and then we're going to save that to our dependencies so press enter to install this and then once that's done we can check it out in package.json yep it's right here so this is what we're going to be using ultimately to connect to our mongodb database and we'll see how to do that in the next lesson

Original Description

🐱‍💻 View this course & other premium courses without ads on the Net Ninja Pro site: https://net-ninja-pro.teachable.com/p/net-ninja-pro/ 🐱‍💻 Access the course files on GitHub: https://github.com/iamshaunjp/complete-mongodb (lesson code available from lesson 15 onwards) 🐱‍💻 Modern JavaScript Course: On Net Ninja Pro - https://netninja.dev/p/modern-javascript-from-novice-to-ninja On Udemy - https://www.thenetninja.co.uk/udemy/modern-javascript On YouTube - https://www.youtube.com/watch?v=iWOYAxlnaww&list=PL4cUxeGkcC9haFPT7J25Q9GRB_ZkFrQAc 🐱‍💻 Node.js Crash Course: On Net Ninja Pro - https://netninja.dev/p/node-js-crash-course On YouTube - https://www.youtube.com/playlist?list=PL4cUxeGkcC9gcy9lrvMJ75z9maRw4byYp 🐱‍💻 VS Code - https://code.visualstudio.com/ 🐱‍💻 Download MongoDB - https://www.mongodb.com/try/download/community
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Net Ninja · Net Ninja · 0 of 60

← Previous Next →
1 Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
2 Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
3 Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
4 GraphQL Tutorial #1 - Introduction to GraphQL
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
5 GraphQL Tutorial #2 - A Birdseye View of GraphQL
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
6 GraphQL Tutorial #3 - Project (stack) Overview
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
7 GraphQL Tutorial #4 - Making Queries (front-end preview)
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
8 GraphQL Tutorial #5 - Express App Setup
GraphQL Tutorial #5 - Express App Setup
Net Ninja
9 GraphQL Tutorial #6 - Setting up GraphQL
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
10 GraphQL Tutorial #7 - GraphQL Schema
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
11 GraphQL Tutorial #8 - Root Query
GraphQL Tutorial #8 - Root Query
Net Ninja
12 GraphQL Tutorial #9 - The Resolve Function
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
13 GraphQL Tutorial #10 - Testing Queries in Graphiql
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
14 GraphQL Tutorial #11 - GraphQL ID Type
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
15 GraphQL Tutorial #12 - Author Type
GraphQL Tutorial #12 - Author Type
Net Ninja
16 GraphQL Tutorial #13 - Type Relations
GraphQL Tutorial #13 - Type Relations
Net Ninja
17 GraphQL Tutorial #14 - GraphQL Lists
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
18 GraphQL Tutorial #15 - More on Root Queries
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
19 GraphQL Tutorial #16 - Connecting to mLab
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
20 GraphQL Tutorial #17 - Mongoose Models
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
21 GraphQL Tutorial #18 - Mutations
GraphQL Tutorial #18 - Mutations
Net Ninja
22 GraphQL Tutorial #19 - More on Mutations
GraphQL Tutorial #19 - More on Mutations
Net Ninja
23 GraphQL Tutorial #20 - Updating the Resolve Functions
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
24 GraphQL Tutorial #21 - GraphQL NonNull
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
25 GraphQL Tutorial #22 - Adding a Front-end
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
26 GraphQL Tutorial #23 - Create React App
GraphQL Tutorial #23 - Create React App
Net Ninja
27 GraphQL Tutorial #24 - Book List Component
GraphQL Tutorial #24 - Book List Component
Net Ninja
28 GraphQL Tutorial #25 - Apollo Client Setup
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
29 GraphQL Tutorial #26 - Making Queries from React
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
30 GraphQL Tutorial #27 - Rendering Data in a Component
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
31 GraphQL Tutorial #28 - Add Book Component
GraphQL Tutorial #28 - Add Book Component
Net Ninja
32 GraphQL Tutorial #29 - External Query File
GraphQL Tutorial #29 - External Query File
Net Ninja
33 GraphQL Tutorial #30 - Updating Component State
GraphQL Tutorial #30 - Updating Component State
Net Ninja
34 GraphQL Tutorial #31 - Composing Queries
GraphQL Tutorial #31 - Composing Queries
Net Ninja
35 GraphQL Tutorial #32 - query variables
GraphQL Tutorial #32 - query variables
Net Ninja
36 GraphQL Tutorial #33 - Re-fetching Queries
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
37 GraphQL Tutorial #34 - Book Details Component
GraphQL Tutorial #34 - Book Details Component
Net Ninja
38 GraphQL Tutorial #36 - Styling the App
GraphQL Tutorial #36 - Styling the App
Net Ninja
39 GraphQL Tutorial #35 - Making a Single Query
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
40 Build Apps with Vue & Firebase - Udemy Course
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
41 Updated Vue & Firebase Course (Udemy)
Updated Vue & Firebase Course (Udemy)
Net Ninja
42 Vue & Firebase Real-time Chat (Preview) #1 - Intro
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
43 Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
44 Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
45 Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
46 Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
47 Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
48 Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
49 Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
50 Object Oriented JavaScript Tutorial #1 - Introduction
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
51 Object Oriented JavaScript Tutorial #2 - Object Literals
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
52 Object Oriented JavaScript Tutorial #3 - Updating Properties
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
53 Object Oriented JavaScript Tutorial #4 - Classes
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
54 Object Oriented JavaScript Tutorial #5  - Class Constructors
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
55 Object Oriented JavaScript Tutorial #6 - Class Methods
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
56 Object Oriented JavaScript Tutorial #7 - Method Chaining
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
57 Object Oriented JavaScript Tutorial #8 - Class Inheritance
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
58 Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
59 Object Oriented JavaScript Tutorial #10 - Prototype
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
60 Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja

This video tutorial teaches how to use MongoDB drivers to communicate with MongoDB on a programmatic level, using the Node driver and Express framework. It covers installing and using nodemon, connecting to a MongoDB database, and creating an endpoint to communicate with MongoDB.

Key Takeaways
  1. Initialize a new node project using npm init
  2. Create a new entry file for the project called app.js
  3. Install the Express package using npm install express
  4. Require Express in the app.js file
  5. Initialize the Express app
  6. Use the listen method to listen for requests on port 3000
  7. Set up a get request handler for the /books route and send a JSON response with a welcome message
  8. Install nodemon globally with npm install -g nodemon
  9. Use nodemon to automatically restart the server when file changes are detected
  10. Install the MongoDB driver for Node with npm install mongodb
💡 Using MongoDB drivers and the Express framework allows for efficient communication with MongoDB and building of web applications in Node.js.

Related Reads

Up next
Indian Express Editorial Analysis by Chandan Sharma - 1 JULY 2026 | UPSC Current Affairs 2026
StudyIQ IAS
Watch →