02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoPrevious 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.

Installing MongoDB

Learn how to install MongoDB, including prerequisites and key steps for setting up the database on your machine.

10:05

Creating a Mongo Database

This video guides you through creating your first MongoDB database using the built-in driver.

04:21

Inserting Collections and Documents in MongoDB

Learn how to insert collections and documents in MongoDB, and understand the core concepts of collections, documents, and database structure.

09:07

Adding is great, but how do you find things

Learn how to effectively search and retrieve documents in MongoDB collections, using commands like find, pretty, and limiting results.

05:12

Finding complicated things with operators

Learn how to use advanced operators in MongoDB to find items more accurately.

07:03

Deleting Documents, Collections and Databases

Learn how to properly delete documents, collections, and databases in MongoDB. Understand best practices and methods like deleteOne(), deleteMany(), and drop().

03:09

mongoDB Driver in NodeJS

Learn how to integrate MongoDB driver with Node.js and perform bulk operations. Final section in the full stack series.

16:05