MongoDB Database Commands

Have a Database Problem? Speak with an Expert for Free
Get Started >>

Introduction

If you’re just getting started with MongoDB, you may not yet know all the key database commands by heart. It can be helpful to have a reference guide available to list and explain these commands until you have them memorized. This article can serve as a “cheat sheet” of sorts, discussing some of the most commonly-used MongoDB database commands. These essential commands enable users to interact with a MongoDB database to create collections and documents.

Prerequisite

Before going any further with this tutorial, please make sure that MongoDB is already installed and configured on your machine.

Why Use MongoDB Database Commands?

Let’s begin our discussion of MongoDB database commands by looking at some of the overall benefits of MongoDB:

  • MongoDB offers flexibility in terms of data modeling. Documents in a collection don’t need to have the same fields or even the same structure.
  • MongoDB offers faster performance than a traditional relational database.
  • MongoDB provides efficiency due to it’s schema-less nature.
  • Auto-sharding is a feature bundled with MongoDB which distributes data among multiple physical partitions called shards. This efficient approach to scalability allows you to keep up with data growth with little effort.

Basic MongoDB Database Command

Now that we’ve provided a brief overview of MongoDB and its benefits, let’s take a look at some MongoDB database commands and how to use them.

Showing Existing Database

MongoDB provides a simple way to show all existing databases with the command show dbs. This command sllows the user to select a database with which they want to work.

1
2
3
4
5
6
7
8
9
10
11
> show dbs
EcommerceDb   0.000GB
StudentDb     0.000GB
admin         0.000GB
config        0.000GB
customerinfo  0.000GB
employeeinfo  0.000GB
hoteldb       0.000GB
housedb       0.000GB
local         0.000GB
mobiledb      0.000GB

You can see that the system we’re using for this tutorial has a few existing databases.

Create Database Command

Next, let’s look at the command used to create a MongoDB database. The syntax of this command is: use <database_name>. The MongoDB use followed by the database name will create the database if it doesn’t already exist; otherwise, the existing database will be returned.

To use this command, you need to be logged in to your Mongo shell:

1
use mydatabase

Mongo shell will notify you that it has switched to the defined database: switched to db mydatabase

1
2
> use mydatabase
switched to db mydatabase

Drop Database Command

In the previous section, we showed you how easy it is to create a MongoDB database. Now, let’s talk about how to delete a database with a simple two-step approach:

  • First, select the database you want to delete, or drop, with the use command:
1
2
use mobiledb
switched to db mobiledb
  • Then perform the drop operation using the db.dropDatabase() command:
1
2
db.dropDatabase()
{ "dropped" : "mobiledb", "ok" : 1 }

We receive a notification letting us know that MongoDB ‘dropped’ the ‘mobiledb’.

To verify that the delete operation was successfully, we execute the show dbs command again. The database we dropped should not show up in the list of existing databases:

1
2
3
4
5
6
7
8
9
10
> show dbs
EcommerceDb   0.000GB
StudentDb     0.000GB
admin         0.000GB
config        0.000GB
customerinfo  0.000GB
employeeinfo  0.000GB
hoteldb       0.000GB
housedb       0.000GB
local         0.000GB

Create Collection Command

In this section, we’ll show you how to create a MongoDB collection. We’ll be using this basic syntax: db.createCollection(collection_name).

1
db.createCollection("your_desired_collection_name").

Drop MongoDB Collection

The process of dropping a MongoDB collection is similar to the process of dropping a database:

  • First, we select the database where the collection is stored with the use command:
1
2
use mydatabase
switched to db mydatabase
  • We then show the existing collections within a database using the command show collections:

  • Next, we select the collection and proceed with the drop operation:

1
2
db.samplecollection.drop()
true

Mongo shell notifies us that the samplecollection was successfully dropped.

INSERT Document into a MongoDB Collection

Now that we know how to create and delete a MongoDB collection, let’s try to insert a document into our target MongoDB collection.

The basic syntax for inserting into a MongoDB collection is: db.<collection_name>.<INSERT>(BSON).

1
2
3
4
5
6
7
db.samplecollection.insertOne(
   { "key1" : "value",
     "key2" : value,
     "key3" : ["value"],
     "key4" : { "a" : 28, "b" : 3, "c" : "cm" }
   }
)

The inserted document can be as simple or as complex as you desire due to the schema-less nature of MongoDB.

Query MongoDB

In this section, we’ll take a look at the process of querying a MongoDB document.

The basic form of the command used for queries is: db.<collection_name>.find().pretty().

The find() command will find the all the existing documents located in the target MongoDB collection.

The pretty() command will instruct Mongo shell to display the result in a BSON format for readability.

Conclusion

If you’re a relatively new user of MongoDB, it’s important to get acquainted with some of the essential commands used for database administration. In this article, we provided a handy reference guide to MongoDB database commands, complete with examples of the most commonly-used commands. Although we covered a number of commands in this tutorial, there are still many more useful commands for you to explore in your own research.

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.