Video Transcript (English)
Hi there! In this video, you'll learn how to use the .mongoshrc.js file to add functionality to the MongoDB Shell, also known as `mongosh`.
We’ll explore how to use the .mongoshrc.js file to add custom helper functions and customize the prompt to include additional information about our database.
On startup, `mongosh` checks your home directory for a hidden file called `.mongoshrc.js` and reads its contents before displaying the prompt for the first time. This file opens up all sorts of possibilities for adding functionality and customization.
For example, as a database administrator, you might want to check if the server is running a compatible version for a given client. For this we can use a built-in helper command called `db.adminCommand()` which runs commands against the `admin` database. When you specify a command that you want to run against the admin database, the command should be passed as a document. The command name should be one of the keys in the document and, in most cases, its value should be set to 1. To confirm this, you should always check the details of the command in question.
For commands that don’t require additional parameters, we take a shortcut by passing the command name as a string instead of a document to `db.adminCommand(“CommandName”)`.
Now let’s go back to checking the compatibility version of the MongoDB server. This can help determine if there will be issues with incompatible clients.
Our command starts with db.adminCommand(). Then we pass in the command document like this.
The `getParameter` command is used to get specific values for a parameter by setting its value to 1. The second field is the parameter value that we want to retrieve. In this case, we want the`featureCompatibiltyVersion`, so we also set its value to 1.
After running this command, we can see that the output contains the feature compatibility version of the server.
But one issue is that the command is pretty long. So, let’s add a helper function called `fcv()` to the `.mongoshrc.js` file to act as a shortcut. This will make the command much easier to run.
The `.mongoshrc.js` file won’t exist by default, so let’s create it in our home directory. Then we can add our helper function.
Here I am creating the .mongoshrc.js file and opening it in the editor of my choice.
Now I am adding the function to the .mongoshrc.js file … saving … and then exiting
Now that we have created the `.mongoshrc.js` file and added the helper function, let’s test it!
We need to exit and then restart `mongosh` to load the new `.mongoshrc.js` content.
Next, we’ll run the helper function. We get the same result as if we ran the command directly. The only difference is that this time we had much less to type.
Helper functions are just one thing that we can add to the .mongoshrc.js file. We can also use this file to customize our prompt.
The current prompt displays some basic information, but we can put additional information here.
We can use native methods in `mongosh` to get information about the MongoDB server that we’re connected to and display that information in the prompt.
For example, let’s add a new function to our `.mongoshrc.js` file called `prompt()`. In the `prompt()` function, we’ll add some variables to get information such as (switch to slides in the video) the name of the database, whether we’re connected to Atlas, the read preference, and the current user, to name a few. (back to code) We can access this information by using the `db` object in `mongosh`.
With the variables set, now all we need to do is return a string that contains the information from those variables.
After making these modifications, we’ll restart the shell, connect to Atlas, and see what happens!
We now have a more informative prompt. It shows that we’re connected to Atlas, that the name of the database is `sample_analytics`, the current user is myAtlasDBUser, and that the read preference is primary.
Now that you have seen what’s possible with the `.mongoshrc.js` file, you can add and customize mongosh to make it work better for you.
In this video, you learned how to add functionality to mongosh by editing the `.mongshrc.js` file. Specifically, you learned how to add custom helper functions for lengthy commands, as well as how to customize the prompt to include more information about the database or local deployment.