Bird
Raised Fist0
MongoDBquery~30 mins

MongoDB Shell (mongosh) basics - Mini Project: Build & Apply

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
MongoDB Shell (mongosh) Basics
📖 Scenario: You are working as a data assistant for a small bookstore. You need to organize the book information in a MongoDB database so the store can easily find and update book details.
🎯 Goal: Build a simple MongoDB collection called books using the mongosh shell. You will insert book data, set up a query filter, find books by a condition, and finally update a book's information.
📋 What You'll Learn
Create a books collection with three book documents
Add a variable to filter books by a minimum number of pages
Use a find() query with the filter to get books with pages greater than or equal to the threshold
Update the price of a specific book using updateOne()
💡 Why This Matters
🌍 Real World
Bookstores and libraries use MongoDB to store and manage book information for easy searching and updating.
💼 Career
Knowing how to use the MongoDB shell to insert, query, and update data is essential for database administrators and backend developers working with NoSQL databases.
Progress0 / 4 steps
1
Create the books collection with initial data
In the mongosh shell, insert three book documents into the books collection with these exact fields and values: { title: 'The Hobbit', author: 'J.R.R. Tolkien', pages: 310, price: 15 }, { title: '1984', author: 'George Orwell', pages: 328, price: 12 }, and { title: 'To Kill a Mockingbird', author: 'Harper Lee', pages: 281, price: 10 }. Use the insertMany() method on db.books.
MongoDB
Hint

Use db.books.insertMany() with an array of objects for the books.

2
Set a page count filter variable
Create a variable called minPages and set it to 300. This will be used to filter books with pages greater than or equal to this number.
MongoDB
Hint

Use const minPages = 300 to create the variable.

3
Find books with pages >= minPages
Use db.books.find() with a filter object that selects books where the pages field is greater than or equal to minPages. Assign the result to a variable called booksWithMinPages.
MongoDB
Hint

Use { pages: { $gte: minPages } } as the filter in find().

4
Update the price of '1984' to 14
Use db.books.updateOne() to find the book with title equal to '1984' and update its price field to 14. Use the $set operator.
MongoDB
Hint

Use updateOne() with a filter and $set to change the price.

Practice

(1/5)
1. What command in mongosh lists all the databases available on the server?
easy
A. show dbs
B. list databases
C. show collections
D. dbs list

Solution

  1. Step 1: Understand the command purpose

    The question asks for the command that lists all databases in mongosh.
  2. Step 2: Recall mongosh commands

    show dbs is the correct command to list databases. Other options are incorrect or do not exist.
  3. Final Answer:

    show dbs -> Option A
  4. Quick Check:

    List databases = show dbs [OK]
Hint: Use 'show dbs' to list all databases quickly [OK]
Common Mistakes:
  • Confusing 'show collections' with listing databases
  • Using non-existent commands like 'list databases'
  • Trying 'dbs list' which is invalid
2. Which of the following is the correct syntax to switch to a database named shop in mongosh?
easy
A. switch shop
B. use shop
C. db switch shop
D. change db shop

Solution

  1. Step 1: Identify the command to change database

    In mongosh, the command to switch databases is use followed by the database name.
  2. Step 2: Match the correct syntax

    use shop correctly switches to the 'shop' database. Other options are invalid commands.
  3. Final Answer:

    use shop -> Option B
  4. Quick Check:

    Switch database = use [OK]
Hint: Remember: 'use' changes the current database [OK]
Common Mistakes:
  • Using 'switch' instead of 'use'
  • Adding extra words like 'db' or 'change'
  • Typing commands that don't exist in mongosh
3. Given the following commands in mongosh:
use testdb
db.products.insertOne({name: 'Pen', price: 1.5})
db.products.find({name: 'Pen'}).toArray()

What will be the output of the find() command?
medium
A. [{ _id: ObjectId("...") , name: 'Pen', price: 1.5 }]
B. null
C. SyntaxError
D. []

Solution

  1. Step 1: Insert a document into 'products' collection

    The insertOne() command adds a document with name 'Pen' and price 1.5 to the 'products' collection.
  2. Step 2: Query the collection for documents with name 'Pen'

    The find() command searches for documents matching {name: 'Pen'}. Since we inserted one, it returns an array with that document including an auto-generated _id.
  3. Final Answer:

    [{ _id: ObjectId("...") , name: 'Pen', price: 1.5 }] -> Option A
  4. Quick Check:

    Inserted doc found = array with document [OK]
Hint: Inserted documents appear in find results as arrays [OK]
Common Mistakes:
  • Expecting find() to return null if document exists
  • Confusing syntax causing errors
  • Thinking find() returns a single object, not an array
4. You run this command in mongosh:
db.users.insertOne({name: 'Alice', age: 30})

But get an error: ReferenceError: db is not defined. What is the most likely cause?
medium
A. The syntax of the insertOne command is incorrect
B. The insertOne method does not exist
C. You forgot to switch to a database using use command
D. MongoDB server is not running

Solution

  1. Step 1: Understand the error message

    ReferenceError: db is not defined means the shell does not know what db is.
  2. Step 2: Identify cause in mongosh context

    This usually happens if you have not selected a database with use. Without a current database, db is undefined.
  3. Final Answer:

    You forgot to switch to a database using use command -> Option C
  4. Quick Check:

    db undefined = forgot 'use' [OK]
Hint: Always run 'use dbname' before using db commands [OK]
Common Mistakes:
  • Assuming insertOne method is missing
  • Thinking syntax is wrong when it is correct
  • Ignoring the need to start MongoDB server
5. You want to list all collections in the current database and then insert a document into a collection named orders. Which sequence of commands in mongosh is correct?
hard
A. show collections use orders db.insertOne({item: 'Book', qty: 3})
B. show dbs db.orders.insertOne({item: 'Book', qty: 3})
C. use orders show collections db.insertOne({item: 'Book', qty: 3})
D. show collections db.orders.insertOne({item: 'Book', qty: 3})

Solution

  1. Step 1: List collections in current database

    show collections lists all collections in the current database. This is the first correct step.
  2. Step 2: Insert document into 'orders' collection

    Use db.orders.insertOne(...) to insert a document into the 'orders' collection. This is the correct syntax.
  3. Final Answer:

    show collections db.orders.insertOne({item: 'Book', qty: 3}) -> Option D
  4. Quick Check:

    List collections then insert = show collections db.orders.insertOne({item: 'Book', qty: 3}) [OK]
Hint: Use 'db.collection.insertOne()' after 'show collections' [OK]
Common Mistakes:
  • Trying to 'use' a collection as a database
  • Calling insertOne directly on db without collection
  • Listing databases instead of collections