Adding data with MongoDB Compass

Using the Compass app to create a database and collection in MongoDB, and to import data from a JSON file.

Learning Goals

At the end of this Tutorial, you will be able to:

  • Use the Compass app to create a database and collection in MongoDB.
  • Import a JSON file into a collection to create documents in a collection.
  • View your data with the MongoDB extension for VS Code.

About MongoDB Compass

The free Compass app provides an easy-to-use interface for interacting with databases, collections and documents on MongoDB Atlas.

You downloaded and installed Compass in a previous Tutorial using your provided connection string.

Creating a database and collection

Follow the steps below:

  1. Download the soccer_players.json file.
  2. Launch the Compass app and connect it to your account on MongoDB Atlas. You should see a screen similar to that below. screenshot
  3. In the left column, beside the Databases heading, click the plus (+) icon. screenshot
  4. Enter the names for your database and collection. For example, db_soccer_players and soccer_players. screenshot You can add further collections to your new database later, if required.
  5. Click the Create Database button.

Now, you are ready to add data to your database and collection.

Importing JSON data

With the soccer_players collection selected in the left column:

  1. Click the dropdown arrow at the right of the ADD DATA button and select Import JSON or CSV file. screenshot
  2. Select the soccer_players.json file and, on the next screen, click the Import button. screenshot
  3. You should now see a screen similar to the following. screenshot

Note that MongoDB has automatically added a field named _id to each document.

Viewing data with VS Code extension

To view your database and collection with the VS Code extension for MongoDB, follow the steps below:

  1. In the left Activity Bar, click the MongoDB extension icon.
  2. Click and expand the db_soccer_players database, soccer_players collection, and individual documents. See the sample screen below. screenshot

In the next Tutorial, you will build an Express app to work with this MongoDB data.