MongoDB Database Basics Tutorial

In this MongoDB tutorial we learn how to create and use a database, as well as other common operations like deleting a database, listing all databases and checking which database is currently selected.

Here's a table of contents of what you'll learn in this lesson:
(click on a link to skip to its section)

Let's jump right in.

How to create or select a database

To create a new database in MongoDB, we run the use command.

Syntax:
use database_name
Example:
use mydb

If the database has been created, Mongo will switch to the new database and return the following message.

Output:
switched to db mydb

If we run the use command on a database that already exists, it will simply switch over to the new database.

The use command is also used to select a database by switching to it.

If we run the command with another database that already exists on our server, it will switch to it.

Example:
use admin
Output:
switched to db admin

If we wanted to switch back to the mydb database, we simply use it again.

Example:
use mydb

Database naming restrictions

When we name our database in MongoDB, we should consider the following restrictions.

  • Database names may not contain null characters and may not be empty
  • Database names may not be longer than 64 characters
  • Database names may not contain the following special characters / \ . " $ * : | ?

How to check which database is selected

We can see which database is currently selected (used) if we run the db command.

Example:
db

The terminal will return the name of the selected database.

Output:
mydb

How to show a list of all databases on the server

If we want to show a list of all the databases on the server, we use the show dbs command.

Example:
show dbs

The terminal will return a list of the databases that exist on the server as well as their current size.

Output:
admin   0.000GB
config  0.000GB
local   0.000GB

You may have noticed that the database we created earlier isn’t on this list. That’s because Mongo will only show a database if it has at least one document.

Let’s go ahead and insert a document for demonstration purposes.

The following command will look complicated, but don’t worry about it for now. We cover document creation in more detail in the Document CRUD: Create lesson .

Example:
db.tutorial.insert({"name":"MongoDB Tutorial"})

The terminal will return a success message, and if we run the show dbs command again, our database will show up in the list.

Example:
show dbs
Output:
admin   0.000GB
config  0.000GB
local   0.000GB
mydb    0.000GB

How to delete (drop) a database

To delete a database from the server, we use the db.dropDatabase() method.

Syntax:
db.dropDatabase()

The function doesn’t take in any arguments but instead it will delete the currently selected (used) database. If no database is selected, it will delete the ‘test’ database instead.

If we want to delete the ‘mydb’ database for example, we would select it first with the use command.

Example:
use mydb

As we know, this selects the ‘mydb’ database and we should be able to delete it now.

Example:
db.dropDatabase()

If the operation succeeds, the terminal will return the following OK status.

Output:
{ "dropped" : "mydb", "ok" : 1 }

To confirm that the database has been deleted, we can run the show dbs command again.

Output:
admin   0.000GB
config  0.000GB
local   0.000GB

Great, the ‘mydb’ database has been deleted.