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.
How to create or select a database
To create a new database in MongoDB, we run the use command.
use database_name
use mydb
If the database has been created, Mongo will switch to the new database and return the following message.
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.
use admin
switched to db admin
If we wanted to switch back to the mydb database, we simply use it again.
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.
db
The terminal will return the name of the selected database.
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.
show dbs
The terminal will return a list of the databases that exist on the server as well as their current size.
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 .
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.
show dbs
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.
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.
use mydb
As we know, this selects the ‘mydb’ database and we should be able to delete it now.
db.dropDatabase()
If the operation succeeds, the terminal will return the following OK status.
{ "dropped" : "mydb", "ok" : 1 }
To confirm that the database has been deleted, we can run the show dbs command again.
admin 0.000GB
config 0.000GB
local 0.000GB
Great, the ‘mydb’ database has been deleted.