How to Create and Drop a Database in MongoDB

Introduction

If you are new to MongoDB, then you are probably going to start with a new database. Interesting fact: MongoDB doesn’t have a command to create a database. It’s “auto-magically” created the first time a value is saved into a collection. This article will show you how to create both a database and a collection to get you started on your project. Then show you how to delete your database when you are done.

Databases and Collections

  • A MongoDB database stores collections of documents. Each document contains the “data” using fields. Interestingly, each document doesn’t have to contain the same set of fields or data types. It only needs to contain the information necessary for each document.
  • A collection is just a group of MongoDB documents. If you are coming from a SQL background, it is similar to a table. However, in MongoDB there is no schema needed to define what is in a document.

Prerequisites

  • You should have MongoDB installed
  • It’s not required but it’s recommended that you have some previous command line experience with MongoDB.
  • You should have access to the MongoDB command line ( Execute mongo )

Create the Database

Our first step is to switch to a non-existent database and insert data by executing the use statement. If the database doesn’t exist, MongoDB will create it. For this example, we will create a database called recipes:

use recipes

Once the command is executed, you will see this output:

switched to db recipes

But, wait! The database isn’t created until you insert some data. The insert statement will add a document into the collection. Within the statement, both a field name and a field value must be present. Let’s create a collection for sweets and insert a document into the collection:

db.sweets.insert({ name: "Apple Pie" });

If this has been inserted correctly, you will see this response:

WriteResult({ nInserted: 1 });

So let’s take it one step further and show that you don’t have to have the exact same fields in each document. We will add another field to a new document:

db.sweets.insert({ name: "Chocolate Cake", kind: "cake" });

This will result in the same output as before:

WriteResult({ nInserted: 1 });

To check that your database was created use either the show databases or show dbs statement:

> show databases
admin    0.000GB
config   0.000GB
local    0.000GB
recipes  0.000GB

To see the contents of the database use the find() statement:

> db.sweets.find()
{ "_id" : ObjectId("5d660209633b9f3d7a37d24d"), "name" : "Apple Pie" }
{ "_id" : ObjectId("5d660215633b9f3d7a37d24e"), "name" : "Chocolate Cake", "kind" : "cake" }

Drop the Database

Now that you have created a database and verified it exists, let’s delete it. It’s always good practice to switch to the database you want to drop to ensure you delete the right one. To drop a database, you simply execute the db.dropDatabase() command:

> use recipes
switched to db recipes
> db.dropDatabase()
{ "dropped" : "recipes", "ok" : 1 }

You will get the above response back if you dropped the database successfully. Now let’s check it:

> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB

And it’s gone!

Conclusion

In this article we showed you how create and drop a database. Along with that, you learned some useful commands to create a collection, find your database and see the contents within a collection. We hope this was a useful introduction to learning some basics of MongoDB. If you need help managing your database please don’t hesitate to reach out to us at Object Rocket.

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.