Lesson 3: Connecting to MongoDB Servers / Learn

Video Transcript (English)

The MongoDB Shell, or mongosh, is a fully functional Read Evaluate Print Loop environment for interacting with MongoDB deployments.

In this video, you will learn how to use mongosh methods to create a new user and run commands on the admin database by using the db.adminCommand() method.

mongosh provides a number of methods that allow us to interact with mongod, including database and replication methods.

We’ll focus on database methods in this video.

You can use database methods to interact with your data, to manage databases, collections, and users, and to check the status of your database.

Let’s see some of these database methods in action.

Here, we’re connecting to the MongoDB Shell on a new mongod instance, with authorization enabled in the configuration file.

Because authorization is enabled, we need to use the database method createUser() to create the first database user on the admin database with the localhost exception.

The localhost exception allows all connections from the localhost interface to have full access to that instance so that the first user can be created.

This exception applies ONLY when there are no users or roles created in the MongoDB instance.

And, connections that use the localhost exception can create ONLY the first user. So, it’s essential that this user has a role that allows it to create other users.

Let’s create the first user on the admin database. Because we’re working in mongosh, we can use the `use` helper to switch contexts.

Success!

Now let’s use the createUser() method. We’ll set our username to dbaTestAdmin, our password to dbaTestPassword, and our role to “root” on the admin database.

The root role will give us full control over the instance, including the ability to create other users. We strongly recommend that you use stricter roles for your users in keeping with the principle of least privilege.

Now, when we run the “show users” helper, we see that authentication is required to run this command. This means that we successfully created a new user!

We can authenticate by running another mongosh method, db.auth, and passing in our username and the passwordPrompt() method.

We’re prompted to enter the password for this user.

Success.

Now, when we run “show users”, we see our new user!

Nice! We’ve protected our deployment by ensuring that changes can’t be made without authenticating as a user.

Next, let’s discuss the adminCommand() method.

If we’re in mongosh, we can use the `use` helper to switch to the admin database when we want to run a command on the admin database.

We just did this to create a new user.

But mongosh provides a more practical alternative in the form of the adminCommand() method. The adminCommand() method lets us run a command on the admin database, no matter what the context is.

The two code blocks shown here are equivalent, but using adminCommand() is the preferred method for running commands on the admin database.

Let’s try it.

We’ll switch our context to the `test` database.

Now, we’ll use the `shutdown` command with the adminCommand method to stop running mongod.

It worked, even though we’re in the test database. Great!

These are just a few of the available database methods. We can run the help() method on the database object to retrieve more common methods.

Let’s recap what you learned in this video:

First, you learned that the MongoDB Shell, or mongosh, is a fully functional REPL environment for interacting with MongoDB deployments.

mongosh provides a number of methods that allow us to interact with mongod, including database and replication methods

We can use database methods to interact with data, to manage databases, collections, and users, and to check the status of a database.

In this video, we used the database method db.createUser() to create and manage users.

You also learned that db.adminCommand() runs administrative commands on the admin database, no matter the context.

For a full list of MongoDB Shell methods, you can use the help() method on a database object, or visit MongoDB’s documentation.