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

Creating a Mongo Database

Creating a MongoDB Database

In this tutorial, we'll explore how to create and interact with a MongoDB database using the built-in MongoDB shell. Before we begin, make sure you have installed MongoDB on your system. This guide assumes you have MongoDB properly set up and ready to use.

Getting Started with MongoDB

To create a database in MongoDB, we first need to launch the MongoDB shell and make sure the server is up and running. Here's how you can do that:

  1. Check MongoDB Installation:

    • To ensure that MongoDB is properly installed, open a terminal and type:
      mongod
      
    • This command will start the MongoDB server and you should see output indicating that it is waiting for a connection on port 27017 (the default MongoDB port).
  2. Connecting to the MongoDB Shell:

    • Open a new tab in your terminal while keeping the MongoDB server running.
    • In the new terminal tab, type:
      mongo
      
    • This command will open the MongoDB shell, a built-in interactive environment that allows you to manage your databases.

Creating a Database in MongoDB

Now that we have connected to the MongoDB shell, we can start working with databases. MongoDB makes it easy to create and switch between databases.

Switching and Creating Databases

  • In MongoDB, creating a new database is as simple as switching to a database that does not exist yet. You can use the use command to switch between databases or create a new one.

  • To create a new database, just type:

    use MongoSample
    

    If the MongoSample database does not exist, MongoDB will create it for you when you start adding data to it.

  • You can check which database you are currently using by typing:

    db
    

    The terminal will display the current database's name.

The Basics of MongoDB Databases

In MongoDB, you have a few key concepts to understand:

  1. Database: A collection of data, often dedicated to a specific project or application.
  2. Collections: A collection is akin to a table in SQL. Collections are used to group related documents.
  3. Documents: These are the individual records within a collection, represented in BSON format (similar to JSON).

When working with MongoDB, it's typical to create a new database for every distinct application or website, which keeps related data well-contained and easy to manage.

Using the use Command

  • To create or switch to a different database:
    use TestDatabase
    
    If TestDatabase already exists, MongoDB will switch to it. Otherwise, it will create a new one. You can then switch back to MongoSample:
    use MongoSample
    
    MongoDB’s approach to creating databases is dynamic and convenient — you don't have to pre-define a database, and there is no need to define a schema in advance.

Verifying the Database

After switching to the database, if you type db, you should see the name of the current database, such as MongoSample.

Note: Critical Update

  • Since 2016, MongoDB has introduced new features, including Replica Sets and Transactions, which may be relevant for production systems. Always ensure that your MongoDB setup follows best practices for data integrity and high availability.

Summary

In this tutorial, we learned:

  1. How to start the MongoDB server using the mongod command.
  2. How to connect to the MongoDB shell using the mongo command.
  3. How to create and switch between databases using the use command.
  4. The basic components of a MongoDB database, including databases, collections, and documents.

This process gives you a foundational understanding of managing databases in MongoDB. In the next tutorial, we'll learn how to create collections and insert documents into our new database.

Next Steps

Next, we will cover collections and documents in MongoDB, learning how to organize data effectively and store it in our newly created databases.

Tip: MongoDB has updated its shell to mongosh in recent releases, which offers improved features and is the preferred tool for interacting with MongoDB.

Video Transcript

00:01From this lecture onwards, we are assuming
00:03that you have MongoDB installed
00:05and configured on your local operating system.
00:08In this lecture, we're gonna take advantage
00:10of the built-in MongoDB driver, which is called Mongo.
00:15Not surprisingly with it, we're gonna go ahead
00:17and build our first database as we get familiar to the
00:22use method that is used
00:24to specifically change a specific database.
00:27So let's jump right into it.
00:29Let's see how you create
00:30and move into different databases.
00:36So this is a, a very, very quick reminder.
00:39You need to have MongoDB installed at this stage.
00:41So if you have, and don't start this video, go ahead
00:44and make sure that you have MongoDB the database installed.
00:48Okay, so now that you have the database itself installed,
00:51and by the way, not the driver, you don't have
00:53to worry about the drivers yet
00:54'cause we're gonna do that in a following lecture.
00:56For now, we, you just need to have Mongo db.
00:59Now the way you see if you do is go into your terminal
01:01and just type Mongo DB.
01:05And when you type Mongo db, actually Mongo D,
01:08no, no need for the B.
01:09So when you type Mongo D, it's gonna start up the server
01:13and you can see that it's gonna wait
01:14for connection on port 27, 0 17.
01:18What that means is the database is connected and ready
01:21and it's waiting for someone to connect to it
01:23and start interacting with it.
01:25And that's exactly what we're gonna do next.
01:26So just open up another tab in your terminal
01:29and just type Mongo instead of Mongo D.
01:32When you type in Mongo, it's gonna happen,
01:34it's gonna open up a driver, a built in driver
01:37that will enable you to interact directly
01:39with the database itself.
01:41So while this is the database server sitting here
01:44and waiting, you can see that now we have one
01:46connection that is open.
01:47We also wanna, basically we wanna at this stage now
01:50to start interacting with Arm Mongo database.
01:54So the first, first, first thing that I want you
01:56to be familiar with, there are basically three things
01:58that you really want to know about in databases in general.
02:02In in Mongo DA databases, really there are three types
02:06of names that you want to know and be familiar with.
02:09One is the database itself.
02:12The database itself is that actually, if you type now db,
02:15you're going to get back the name of the current database,
02:18not BD, but db.
02:20And you'll see that our current database name is Test,
02:23which is the default database
02:24that is just set even if you don't create a database.
02:28Now, generally speaking, you want
02:30to have a separate database for every website,
02:32for every project that you're working on.
02:34You really are creating a sandbox of information
02:37that is dedicated for that specific operation.
02:41So if you have a website
02:42and you want data to be able to be shared
02:44between the different modules, then you'd want to keep all
02:47that data in one database.
02:49Alright, so let's go ahead
02:51and create ourself a new database.
02:53It's incredibly easy.
02:54All we have to do is type the word use
02:56and then the name of our database.
02:59You could be any name really.
03:00And in this case I'm gonna call our database name.
03:03I'm gonna call it Mongo Sample.
03:06So we're gonna use Mongo sample
03:08and if I click on enter, it's gonna switch our database
03:11to the Mongo sample.
03:13Now, if we type now db, we're gonna get back that
03:16Mongo sample because we've just created it.
03:19Now we have the MongoDB database. So it was that easy.
03:22With that said, to use access two different ways you could
03:26use test and test already existed so it didn't create it.
03:30And I could go and use back Mongo sample
03:35and by doing that I'm going to revert back
03:37and go back to Mongo samples.
03:39I'm basically moving from one database to the next.
03:41And if that database doesn't exist yet,
03:43then we're just gonna create it on the fly.
03:46Alright, so that's, you've just done
03:48with the first terminology, a first important terminology,
03:51which is called a database.
03:53In the next lecture we're gonna go ahead
03:55and take a look at what collections and what documents are.
04:00In this lecture we learned how to use the mango terminal.
04:05The mango, you could say terminal.
04:06Really it it's a driver terminal.
04:08In the next lecture, we're gonna go ahead and
04:10besides using databases, we're gonna learn
04:13of the next two terms, which is collections
04:16and documents, which
04:17with them we're gonna actually start storing information.
04:20Okay?

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!