The MongoDB Shell

The MongoDB Shell

Overview

In this unit, you'll learn about the MongoDB Shell (mongosh), the command line interface that enables you to interact with local and remote MongoDB deployments. You'll learn how to install mongosh and connect to both Atlas and self-managed deployments. Next, you'll learn how to configure and use mongosh along with its companion library (.mongoshrc.js). Finally, you'll learn some tips and tricks for working with mongosh.

Lesson 1 – Installing and Connecting to the MongoDB Shell

  1. Which of the following commands can be used to connect to a database called students on a local MongoDB instance? (Select one.)

    A. mongosh

    B. mongosh --host localhost --db students

    C. mongosh --db students

    D. mongosh students

  2. Which of the following are valid methods for connecting mongosh to a MongoDB Atlas cluster and selecting the sample_training database? (Select all that apply.)

    A. mongosh "mongodb+srv://<clustername>.mongodb.net/?database=sample_training" –username <username>

    B. mongosh "mongodb+srv://<username>:<password>@<clustername>.mongodb.net/sample_training"

    C. mongosh "mongodb+srv://<clustername>.mongodb.net/" –username <username> --db sample_training

    D. mongosh "mongodb+srv://<clustername>.mongodb.net/sample_training" --username <username>

Lesson 2 – Configuring the MongoDB Shell

  1. What method from the config API allows you to set a configuration option in mongosh? (Select one.)

    • A. config.get()

    • B. config.set()

    • C. config.apply()

    • D. db.config.set()

  2. Which of the following settings can be adjusted by editing an option in the mongosh.conf file? (Select all that apply.)

    A. The color of the font that’s displayed in mongosh

    B. How many items per batch are displayed when using the “it” iterator

    C. The editor used by mongosh when using the edit() method

    D. Whether mongosh prompts the user for confirmation before running a deleteOne() or deleteMany() command

Lesson 3 – Using the MongoDB Shell

  1. You want to use an external JavaScript file within an active mongosh session. What method should you use? (Select one.)

    A. load()

    B. loadjs()

    C. run()

    D. You cannot use external JavaScript files within an active mongosh session.

  2. What method is used to change databases within a script by using the load() method in mongosh? (Select one.)

    A. db.getDb()

    B. db.getMongo()

    C. use

    D. db.getSiblingDB()

Lesson 4 – Using the MongoDB Shell Library (.mongoshrc.js)

  1. Where should the .mongoshrc.js file be located? (Select one.)

    • A. The same directory as the mongosh executable.

    • B. The user's home directory.

    • C. The file must be loaded into the mongosh session by using the load() method.

  2. Which of the following methods terminate an active mongosh session? (Select one.)

    • A. exit

    • B. db.exit()

    • C. db.quit()

Lesson 5 – MongoDB Shell Tips and Tricks

  1. Which of the following examples demonstrates the correct usage of the EJSON.stringify() method in mongosh to convert an extended JSON object into a string? (Select one.)

    A. EJSON.stringify({ name: “Test User”, dob: new Date(“1990-01-01”)})

    B. EJSON.stringify(name: “Test User”, dob: new Date(“1990-01-01”)

    C. ({name: “Test User”, dob: new Date(“1990-01-01”)}).EJSON.stringify()

    D. EJSON.stringify = { name: “Test User”, dob: new Date(“1990-01-01”)}

    A. Option A

    B. Option B

    C. Option C

    D. Option D

  2. In mongosh, what Node.js fs module API method can be used to write the results of a query to a file? (Select one.)

    • A. fs.write()

    • B. fs.writeFileSync()

    • C. fs.commit()

    • D. EJSON.stringify()

  3. You want to load a script into mongosh that requires an npm package. To do so, where should the npm package be installed? (Select all that apply.)

    • A. An option for using an npm package in an external script is to install the package globally and then require it in the script.

    • B. The package can be installed in the node_modules directory in your current working directory. Then it can be added to a mongosh script that can be used with the load() method.

    • C. mongosh will automatically download and install the necessary dependencies when the script is run in the shell with the load() method.