Lesson 5: MongoDB Shell Tips and Tricks / Learn

Video Transcript (English)

Welcome back, team. In this video, we’re going to take our skills to the next level with some tips and tricks from the engineers here at MongoDB for working in mongosh.

Specifically, you'll learn how to use mongosh to write query results to a JSON file. This is an easy way to share a subset of data, or save the results of a diagnostic command, for example. You'll also learn how to generate seed data and store it in a MongoDB database. This is helpful for building a database that you can use for testing your applications or for sharpening your querying skills.

One of the advantages of `mongosh` being built on Node.js is that it gives us access to Node's `fs` module. This module provides an API to interact with the file system that `mongosh` is running on. This means that we can read and write files on the host machine by using JavaScript. This feature also grants us access to npm packages to use in our scripts.

To demonstrate some tips and tricks for working with these features in `mongosh`, we first need data to work with.

Let's start by writing a query that will return some data. We’ll use the `sales` collection in the `sample_supplies` database.

These documents contain some information that we aren’t interested in. So, we will use the find() method and pass in a projection argument so that we return only the fields that we want. The `customer` field seems interesting, so let's return that.

We’ll save these results to a variable called `customers`.

Next, we’ll use the Node `fs` and it’s `writeFileSync` method to write the results to a file called `customers.json`.

We will also use the `EJSON.stringify` method to convert the results to a string before writing them to the file.

After running the command and opening the `customers.json` file, we see that the results were written to the file as expected.

This is a great way to save query results or the output of a diagnostic command to a file for later use.

Another great feature of `mongosh` is that it supports `require()` statements. This means that we can use `npm`, the Node package manager to install packages and use them in our scripts.

In this example, we’ll use the `faker` package to generate a large amount of seed data and store it in MongoDB.

First, let’s install the `faker` package in the same directory as our script. To do this, we’ll use the `npm install` command.

Next, we will require the `faker` package in our script.

Now that the package is available in the script, let's add some code that will generate an object with a fake name, email, phone number, and address. We can use a simple for loop to generate as many users as we want. For now, let's generate 100 users and store them in an array.

Next, we’ll use the `insertMany()` method to insert the users into a new database called `test_data` and a new collection called `users`.

Let's run the script in `mongosh`. Then we’ll use the `countDocuments()` method on the collection to check that the 100 users were inserted correctly.

It appears as though our document count is 100. But to be certain, let’s preview one of the documents.

And there we have it! With just a few lines of code, we have successfully generated some seed data and stored it in MongoDB. You can use scripts like this in mongosh to test queries directly on your MongoDB database. However, it should be noted that while mongosh is great for testing, it shouldn’t be used for developing full applications.

In this video, you learned some tips and tricks for working with `mongosh`. You learned how to write query results to a JSON file by using the Node API inside `mongosh`. You also learned how to generate seed data by using the `faker` package and storing it in a MongoDB database.