02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Installing MongoDB

Installing MongoDB: A Beginner's Guide

Welcome to this new section where we explore one of the essential databases for modern web applications: MongoDB. In this tutorial, we'll cover how to install MongoDB and explain the core concepts to help you get started using it efficiently.

Overview

MongoDB is a popular NoSQL database known for its flexibility, scalability, and the ability to store data in a schema-less, document-like structure (i.e., JSON). Unlike traditional relational databases like MySQL, MongoDB allows you to add and manipulate data without strictly defining schemas. This makes it an excellent choice for projects where requirements change frequently or for developers looking for a quick, flexible database solution.

In this tutorial, we will:

  • Install MongoDB using Homebrew on a macOS environment.
  • Set up the necessary directory for MongoDB's database storage.
  • Discuss key concepts about NoSQL databases and why MongoDB is different from relational databases.

Note: This video was originally created in 2016. MongoDB has since seen several updates, and newer versions may include additional features or altered installation instructions. Please refer to the latest official MongoDB documentation for the most current steps.

Step 1: Understanding NoSQL and MongoDB

Traditional relational databases, like MySQL, have rigid structures. They require a well-planned schema, and any modification to it can involve complex changes. MongoDB, a NoSQL database, provides a more dynamic approach to storing data, making it ideal for scenarios where your data structure evolves.

With MongoDB, you can:

  • Store information without pre-defining strict tables and columns.
  • Easily scale and modify data structures as your application grows.
  • Manage data as collections of documents, which is closer to a real-world representation, making it easy to work with.

Step 2: Installing Homebrew (macOS)

For this guide, we assume you are using macOS. To install MongoDB, you'll first need Homebrew, a package manager for macOS. Homebrew allows you to easily install a wide range of software, including MongoDB.

Installing Homebrew

  1. Open Terminal and type:
    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
    
  2. Press Enter and type in your password when prompted. Homebrew will begin the installation.
  3. Once installed, verify the installation by typing:
    brew -v
    
    You should see the Homebrew version number if the installation was successful.

Step 3: Installing MongoDB

Now that Homebrew is installed, let's use it to install MongoDB:

  1. Install MongoDB via Homebrew:
    brew install mongodb-community
    
  2. After installation, MongoDB requires a folder to store its databases. By default, MongoDB expects to use the /data/db folder. If this folder doesn't exist, MongoDB won't start.
  3. Create the required directory:
    sudo mkdir -p /data/db
    
  4. Change the ownership of the directory so that your user account can access it:
    sudo chown -R $(whoami) /data/db
    
  5. With this directory set up, you can now start MongoDB by running:
    mongod
    
    MongoDB will start and listen on the default port (27017).

Note: In recent MongoDB versions, the installation process may require slightly different commands, especially on newer macOS versions. Please refer to MongoDB's official installation guide for updated instructions.

Step 4: MongoDB Drivers and Node.js Integration

After installing MongoDB, it's time to integrate it into your application. MongoDB can be accessed via a driver. A driver is a piece of software that helps different programming languages interact with MongoDB.

In the next lecture, we will discuss the MongoDB Node.js driver, which allows us to connect MongoDB to Node.js applications. The integration between Node.js and MongoDB makes it easy to interact with the database directly in JavaScript, providing a seamless full-stack JavaScript experience.

Critical Update: The MongoDB Node.js driver has evolved since 2016. Make sure to install the latest version using:

npm install mongodb

Follow the updated API for proper connection management as there have been significant changes to ensure better performance and reliability.

Conclusion

In this tutorial, we successfully installed MongoDB on macOS using Homebrew. We set up the necessary environment and learned the basics of NoSQL databases, such as how they differ from traditional SQL-based databases. MongoDB allows for more flexible and scalable applications, making it an ideal choice for modern web applications.

In the next tutorial, we will explore using the MongoDB Node.js driver to create and manage our first MongoDB database. Stick around as we take our first steps into working with collections and documents in MongoDB!

Video Transcript

00:00Welcome back to a new section
00:02where we're gonna explore another fundamental, fun,
00:05fundamental topic, which is MongoDB.
00:09In this section, we're gonna learn how to install MongoDB,
00:13create a MongoDB database.
00:15We're gonna differentiate and understand the difference
00:17between a database collection document,
00:21where we're gonna go ahead
00:22and insert documents, add content into our database,
00:26and what we're gonna look how to find things,
00:29including complex ways to find things.
00:31And we're gonna wrap things up by learning how to delete all
00:34of these new terms that we've talked about, which are
00:37documents, collections, and databases.
00:40We won't be over there because there's a few more topics.
00:43One is we're gonna take, well, it's gonna be a long video,
00:46the last, last video where we're gonna take a spin on
00:49a MongoDB driver.
00:51And by the way, we're gonna understand the differences
00:52between a driver and a database
00:54and take a look at creating a simple application using
00:58MongoDB driver in no js.
01:00With that said, without mouthful of content
01:03that we're gonna be covering in this section,
01:05let's jump right into the first lecture of this section.
01:08And in this first lecture, we're gonna go ahead
01:10and install MongoDB.
01:12But before we do that, I want to take a second
01:15to talk about no SQL.
01:17Traditionally in server side languages,
01:21databases were planned, structured and quite complex.
01:26If you take a look at my SQL,
01:28there is a big learning curve there beyond the fact
01:31that is very rigid and
01:32whenever you wanna make a change to it, it demands a lot
01:35of changes within your application as well.
01:38The concept of NO SQL comes from the idea of, instead
01:42of structuring your data, wouldn't it be nice to be able
01:45to dynamically save data, whatever data you want, manipulate
01:49and change it without needing
01:50to pre-plan a structure in advance.
01:53What that gives you is the capability of changing structures
01:56and evolving your structure
01:58as you're going on without needing to make major changes
02:01with different languages
02:02and configurations that you're probably less familiar with
02:05that would demand you to involve more people beyond the fact
02:08that this is a really fast
02:10and good solution for a lot of usages online.
02:14Namely, when you have a lot of content related directly
02:16to a certain user, for example, where you don't need to go
02:19through and don't wanna go through a hundred different
02:22databases, databases, or tables and columns to fetch
02:26and gather all those informations.
02:29Alright, so with that said, let's go ahead
02:31and install the MongoDB.
02:33Now, the installation process is different than all the
02:36installations we've done within UM, NPM,
02:39and the reason is is because MongoDB is a database.
02:43It is, it is not a no JS interpretation.
02:47It is an actual database, an actual application that needs
02:50to live on its own sphere.
02:52Once we have that application installed on its own sphere,
02:55then we'll also install through
02:58NPMA driver that knows how to control
03:01and manipulate MongoDB.
03:03So you need to make a very clear separation.
03:05So make sure if you ever go into the documentations
03:07of anything to realize if you're reading documentations
03:10of a driver, which means it's something that is driving
03:13and controlling MongoDB versus documentation
03:17that are directly for MongoDB,
03:19which are two completely different things
03:21that could be very easily confusing if you don't
03:24differentiate between those two.
03:26With that said, let's go ahead and install Mongo db.
03:32So you're watching this video,
03:33that means you're running on a Mac.
03:35Now, even if you're not joined into this video,
03:38the remainder of the video, unless you really do want to,
03:40and then just skip on to the next video,
03:42just make sure you go into our attached documentation and go
03:46and install MongoDB.
03:48That's what you want to do in this video in this section
03:50before you move on to the next video.
03:52So let's go ahead. I'm on a Mac,
03:54so I'm gonna just go directly into the Mongo DB
03:58installation guide, uh, which you have a tutorial
04:01and all this information, it's really easy.
04:03You're gonna have that Otto in the
04:05links attached to this chapter.
04:07Now, before I actually, I'm gonna start installing Mongo db.
04:10I need to have brew installed on a Mac Now Brew
04:14and I'm just typing Brew sh.
04:16That is their site. Brew is basically a package manager
04:20that is a package manager for Mac.
04:23We call just like we have NPM,
04:25that is a package manager for node js.
04:28Then Home Brew is basically a package,
04:32it's a package manager for your operating system,
04:36enabling you to add a lot of things
04:37that exist in a Linux operating system
04:39that are not available on a Mac.
04:41If you've worked on any Linux device, which I hope you have,
04:44as we store in previous chapters, we were working
04:47with Yum on a, on the server, on Linux
04:50and other servers on our other Linux devices.
04:53If they're not sendo, we'll have a different type
04:55of package manager.
04:57So in this case, we're basically creating a package manager.
05:00Now it's really easy to do that.
05:01What I'm gonna do is I'm gonna copy this whole beautiful
05:04link and I'm just gonna go right into terminal
05:07and in my terminal, Al Terminal, terminal in my terminal.
05:12I'm gonna, I'm gonna click on enter, put that in there
05:15and let it start installing homeroom.
05:17It's gonna tell me what files, where it's gonna add it.
05:19I'm gonna click on return
05:21'cause I'm fine with it installing it.
05:22I'm gonna go ahead and put in notes on my password, my admin
05:26or my user password more accurately.
05:28And it's gonna go through the process
05:29of installing Homebrew.
05:32Once I have Homebrew installed,
05:33I'm gonna have access to brew.
05:36Now BREW is the, IS is the, I would say BREW is comparable
05:41to, um, BREW is comparable to NPM only.
05:46Brew installs things globally for our operating system.
05:49Kind of similar to what uh, NPM does.
05:52But this is specifically focused on packages that are
05:55for Linux packages to make
05:56Things work on on your operating system.
05:59So for example, mo, why you might be asking yourself,
06:02why am I not installing MongoDB directly using NPM?
06:06Because NPM only installs packages
06:09and these packages relate to no js.
06:13Contrary to that, MongoDB is a database.
06:16It could be used by no js,
06:17but it could be used by your operating system,
06:19it could be used by PHP, it could be used
06:21by many other languages.
06:23And MongoDB has drivers,
06:25which we're gonna talk about them in the next lecture.
06:27And with those drivers, it enables us to then interact
06:31between our different languages with MongoDB.
06:35Now, the reason why we're learning MongoDB is
06:37because MongoDB interface,
06:39it stores information in JSON,
06:42which is basically JavaScript.
06:44Okay, with that said, I want to basically,
06:46I wanna check that I have your brew.
06:47So I'm just gonna take CH Brew V
06:49and I'm just gonna see if I'm gonna get some sort
06:51of a response and we could see that we have the version
06:54of Brew Beautiful.
06:55In general, you don't really
06:57have to worry too much about this.
06:58So the version of Home Brew right now is 0.9 0.9,
07:01but what I want to show you is brew just like NPM updates.
07:06So I could update all the applications on brew
07:08or update itself as well.
07:10And it's gonna go through to check as if there is anything
07:12that needs to get refreshes and updates.
07:15If there is anything, it will update it
07:17and if not, it's gonna keep it the way it is.
07:19With that said, let's go ahead and also install Now MongoDB
07:25and MongoDB, we'll go this way.
07:28I, is it DB or D? I'm not sure. Let's try MongoDB.
07:31And if we get an arrow, that means it's Mongo D.
07:33So it's going search, it's looking for MongoDB.
07:36Hopefully, if it doesn't
07:37find anything, it looks like it did.
07:38So that's what we type. Yeah. So Brew install MongoDB.
07:42Beautiful. So we're going give it a few seconds for it
07:45to complete it's installation.
07:47And I'm gonna tell you in in the meantime that one
07:50of the things that MongoDB needs is a folder
07:52where it's gonna actually have all of its, I would say,
07:56where it's gonna have its database itself.
07:59Now, by default that da, it expects the database
08:02to be in the data dash slash DB folder.
08:06If that folder doesn't exist, MongoDB won't work.
08:09So for us to make it work, what we want to do is we want to,
08:12we're not gonna change those default configurations.
08:15If you want to, you're more than encouraged to,
08:17to dig deeper into the documentation of MongoDB.
08:21But what I wanna do is I'm gonna keep it the way it is
08:23and I'm gonna go and create an M key.
08:24I'm gonna create a directory
08:26and I'm gonna make sure that I could
08:28create multiple directories.
08:29'cause data doesn't exist yet and DB doesn't exist.
08:31Notice that I'm putting here a trailing up,
08:34'cause I wanna put this in the folder,
08:36in the in the root folder of everything.
08:39I'm gonna go ahead and press there, enter notice
08:41that I have permission denied.
08:42So I'm gonna go ahead and use here a psdo, making sure
08:45that our route supposedly is doing it.
08:47I'm gonna go ahead and type my, type my password
08:52and type my password.
08:54Beautiful. So I just created the directory.
08:57Now, my only problem that I have now,
08:58even though I've created the directory,
09:00is I don't have access to this directory
09:02because it's a directory of the root.
09:04So what I want to do next is I want to go
09:06to Sudu Change owner, CHO,
09:09and I'm gonna recursively, even though there's nothing
09:11inside there, but just for the sake of if there was anything
09:14inside there, we wanna make sure that we are
09:17gonna change everything to be us.
09:20And I'm basically saying, who am I?
09:22I'm, I'm putting your A function
09:23to retrieve who's the username right now.
09:25And I'm gonna go ahead and set that folder to be our folder.
09:29My folder, Ben Fallon.
09:30And if I go ahead and save this, that's it.
09:32That's all I had to do to configure
09:34and get MongoDB to be installed and running.
09:37Now this is the Mac instructions.
09:39If you're working on Windows, if you're working on a Linux,
09:42make sure to follow the documentation that's
09:44provided, the links that are
09:45provided with the appendixes of this chapter.
09:50In this lecture, we've installed MongoDB
09:53and now we're ready to start working with it.
09:55In the next lecture, we'll be introduced
09:57to the Mongo Built-in Driver.
09:59And with that, create our first MongoDB database.
10:04I.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!