Goglides Dev 🌱

Balkrishna Pandey
Balkrishna Pandey

Posted on • Updated on

MongoDB 101: How to Create, Show, and Drop Databases with Ease

Although many online resources are available for MongoDB, I am creating this course series with a specific objective:

  • To provide a step-by-step guide for creating a MERN stack-based application from scratch.
  • To containerize the application using docker-like container tools
  • To cover the basics of creating and deploying a containerized application in a Kubernetes cluster and more advanced topics such as Ingress controllers, Role-Based Access Control (RBAC), and network policies.

Introduction

MongoDB is a popular NoSQL database widely used in web development, especially with the MERN (MongoDB, Express.js, React, Node.js) stack. In this article, we will cover the installation process for MongoDB and the mongosh Shell and the creation, deletion, and copying of databases using the MERN stack as an example.

Prerequisites

Before we begin, you will need to have the following prerequisites:

  • A computer running a supported operating system (e.g., Windows, macOS, or Linux)
  • Access to an administrator account (for installing MongoDB)
  • A working internet connection

Installing MongoDB

To install MongoDB, follow these steps:

  • Visit the MongoDB download page (https://www.mongodb.com/try/download/community) and download the appropriate version for your operating system.
  • Run the installer and follow the instructions to install MongoDB. You can accept the default settings for most of the options, but make sure to select the "Complete" installation type.
  • After the installation, MongoDB will be installed as a service or daemon, depending on your operating system. You can start or stop the service using the appropriate commands for your operating system.

Installing the mongosh Shell

The mongosh Shell is a powerful tool for interacting with MongoDB databases. To install the mongosh Shell, follow these steps:

  • Visit the mongosh shell download page (https://www.mongodb.com/try/download/shell) and download the appropriate version for your operating system.
  • Run the installer and follow the instructions to install the mongosh Shell. You can accept the default settings for most of the options.
  • After the installation, you can start the mongosh Shell by running the mongosh command in a terminal.

Using the "mongod" and "mongosh" Commands:

The "mongod" command is used to start the MongoDB server. This command accepts several options, including the path to the data directory where the server will store the data files. Here's an example of how to use the "mongod" command with the "--dbpath" option to specify the data directory:

mongod --dbpath ~/mongodb
Enter fullscreen mode Exit fullscreen mode

This will start the MongoDB server and store the data files in the"~/mongodb" directory. Make sure to replace the path with the actual path to your desired data directory.

The "mongosh" command is used to connect to a running MongoDB instance and interact with the database. This command opens up a shell that allows you to run commands and queries against the database.

Here's an example of how to use the "mongosh" command to connect to the local MongoDB instance:

mongosh
Enter fullscreen mode Exit fullscreen mode

This will connect to the default MongoDB instance running on localhost and open up the "mongosh" shell. You will see output something similar to this,

MongoDB Shell

Creating a MongoDB Database:

A database in MongoDB is a container for collections containing documents. To create a new database in MongoDB, we can use the "use" command followed by the database name. Here's how you can create a new database called "mernstack" using the MongoDB shell.

Type the following command in the shell to create a new database called "mernstack":

use mernstack
Enter fullscreen mode Exit fullscreen mode

Note that this command does not create any collections in the database. It simply switches to the "mernstack" database and creates it if it does not already exist.

To confirm that the database has been created, you can type the following command:

db
Enter fullscreen mode Exit fullscreen mode

This command will display the current database's name, which should be "mernstack" in this case.

That's it! You have now created a new database in MongoDB. However, remember that the database will only be created once you add data. MongoDB creates a new database implicitly when you first store data in a collection, so if you want to create a new database but don't want to create a collection just yet, you can create a temporary collection and then delete it later.
It's also worth noting that MongoDB has some reserved database names that cannot be used, such as "admin", "config", and "local". MongoDB uses these databases internally for system processes.

Showing MongoDB Databases

After creating a database in MongoDB, view a list of all the existing databases in the current MongoDB instance. You can use the show dbs command in the MongoDB shell to do this.
The output of the show dbs command will be a list of all the databases in the MongoDB instance, along with their respective sizes. Note that the sizes listed are not the sizes of the data within the databases but rather the sizes of the files that store the data.

admin> show dbs
admin      40.00 KiB
config     72.00 KiB
local      40.00 KiB
mernstack  40.00 KiB
Enter fullscreen mode Exit fullscreen mode

If you created a new database using the "use" command but have not yet added any data, you may not see it listed when you run the show dbs command. MongoDB only creates a new database implicitly when you first store data in a collection within that database.

In addition to the show dbs command, there are several other commands that you can use to view information about the databases, collections, and documents in a MongoDB instance. For example, you can use the "show collections" command to list all the collections in the current database or the db.stats() command to view detailed statistics about the current database.

Depending on the user's permissions, specific databases may not be visible in the show dbs output. For example, MongoDB uses the local database internally and may not be visible to all users.

Deleting a MongoDB Database

Deleting a MongoDB database is a straightforward process. However, it is essential to note that once a database is deleted, all data stored within it will be permanently lost.

Here are the steps to delete a database in MongoDB:

Switch to the database you want to delete by running the following command:

use mernstack
Enter fullscreen mode Exit fullscreen mode

Replace "mernstack" with the name of the database you want to delete.

To delete the database, run the following command:

db.dropDatabase()
Enter fullscreen mode Exit fullscreen mode

This operation will delete the "mernstack" database along with all the data stored within it.

If the operation is successful, MongoDB would return the following output:

{ ok: 1, dropped: 'mernstack' }
Enter fullscreen mode Exit fullscreen mode

Top comments (0)