Query This MongoDB Database on Linux

15 minutes
  • 3 Learning Objectives

About this Hands-on Lab

In this activity, you are working as a Systems Administrator and have been tasked with querying a MongoDB database.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Connect and Create a New Database

Start the mongo shell from the Bash prompt:

mongo

Once in the interactive mongo shell, create a new database:

use demo;
Insert Some Data

Insert the data into a new collection in our demo database:

db.inventory.insertMany([
    { id: 0, name: "David", department: "Engineering", expensecode: 200 },
    { id: 1, name: "Clay", department: "HR", expensecode: 100 },
    { id: 2, name: "Sue", department: "Sales", expensecode: 300 },
    { id: 3, name: "Betty", department: "Marketing", expensecode: 400 },
])
Query the Data

Query the database for all documents:

db.inventory.find( {} )

Update the department to be HR for the document with a name of Betty, and confirm:

db.inventory.updateOne(
    { name: "Betty" },
    { 
      $set: { department: "HR" } 
    }
)

db.inventory.find( { name: "Betty" } )

Additional Resources

You will need to complete the following tasks to accomplish this:

  1. Connect and create a new database:
    • Connect using the mongo shell and create a new database named demo.
  2. Insert some data:
    • Insert the following data into a new collection in our demo database:
      ID,Name,Department,ExpenseCode
      0,David,Engineering,200
      1,Clay,HR,100
      2,Sue,Sales,300
      3,Betty,Marketing,400
  3. Query the data:
    • Query the database for all documents, then update the department to be HR for the document with name of Betty. Make sure to confirm the update succeeded.

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?