Lesson 3: Using the MongoDB Shell / Learn

Video Transcript (English)

Hey there! In this video, you'll learn how to leverage the ability of the MongoDB Shell, or mongosh, to interpret JavaScript.

`mongosh` is built on top of Node.js. This means that we get the full power of the JavaScript language inside the shell.

Today you’ll learn how to write and use JavaScript functions in mongosh. We’ll also go over how to switch between and reference multiple databases within a script, how to load that script into mongosh, and finally, how to edit commands in our editor of choice.

First, let’s cover how to write and use JavaScript functions in mongosh. We’ll start by creating a helper function to convert dates into a format that’s easily parsed by MongoDB.

In MongoDB, it’s best practice to store dates as the ISODate type. However, manual conversion can be tedious, and it leaves room for simple formatting mistakes.

We’ll add a function in mongosh that converts date strings to the ISODate format.

For those of you who are reviewing a transcribed version of this course, or who would like a hard copy, we provide the code snippets for this lesson in the resources section.

Our function is called `giveMeADate`. It will accept three numbers (for year, month, and day) and return a date object.

We add this function to the global scope of mongosh by pasting the function into mongosh and pressing Enter. Notice that the shell prints out the function name. This is a good sign that the function was added to the global scope of the shell.

Now let’s run the function!

After passing in a random date and running the function, an ISODate object is returned.

This is pretty cool, but we don’t want to have to paste the code every time we open a `mongosh` session. If only we could build a library of useful shortcuts and functions that could be loaded when needed.

Luckily, we can do all of this and more with `mongosh`.

How? mongosh has a load() method that lets you load a JavaScript file into the shell. The shell also allows us to switch between databases within scripts by using the db.getSiblingDB() method.

To demonstrate these methods, let’s use a script named “randomPost.js” that will use the db.getSiblingDB() method to switch to the `sample_training` database.

The script performs an aggregation query on the `posts` collection. It then uses the $sample stage to randomly select one document and returns only the body field, which happens to be the post text.

You can try this script in the lab associated with the MongoDB Shell unit, or, as indicated earlier, copy it from the resources section for the unit.

The query result is stored in the `result` variable and is printed to the console by using the built-in print method.

To load the script, we need to pass a relative path to the file like this.

After running the script, a post from the posts collection is printed to the shell.

Finally, let's discuss how to edit commands in the editor of your choice. Specifically, we’ll talk about how to define and add a function or variable to the global scope of `mongosh` in an editor.

If we want to edit how our `giveMeaDate` function works. We can use the `edit` command to open that function in our editor of choice.

To do this, first we’ll double-check that we have an editor set by running `config.get('editor')`.

If this returns null, we can set our editor by running `config.set('editor', 'vim')` and replacing `vim` with our editor of choice. For this example, we’ll use an editor called Neovim, so we’ll run `config.set('editor', 'nvim')`.

After our editor is set, we run `edit` to open a blank file, or we can edit the `giveMeADate` function by running `edit giveMeADate`. Let’s make a small change to the function, and then save and exit the editor.

We can see that the function was updated in the shell. This is helpful when we want to make a change to a multi-line command, or if we want to write a longer command from scratch.

In this video, you learned how to write and use JavaScript functions in mongosh. We created and ran a function that accepted three numbers and returned an ISODate() object within mongosh.

You also learned how to load an external script by using the built-in load() method. In that script,

we used the getSiblingDB() method to change databases and return a random post from a collection.

Finally, you learned how to edit commands in the text editor of your choice. We set a default text editor by using the set() method, and then used the `edit` command to edit a function within that editor.

Great job, everyone.