The MongoDB Shell (mongosh) lets you talk to your database using simple commands. It helps you see, add, or change data easily.
MongoDB Shell (mongosh) basics
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
MongoDB
mongosh // Show databases show dbs // Switch to a database use databaseName // Show collections (tables) show collections // Find documents in a collection db.collectionName.find() // Insert a document db.collectionName.insertOne({key: "value"})
mongosh is the command to start the MongoDB shell.
Commands like show dbs and use help you navigate databases.
Examples
MongoDB
show dbs
MongoDB
use myDatabase
MongoDB
show collections
MongoDB
db.users.find()
Sample Program
This example connects to MongoDB, switches to 'testDB', shows collections, adds a product, and then shows all products.
MongoDB
mongosh
show dbs
use testDB
show collections
db.products.insertOne({name: "Pen", price: 1.5})
db.products.find()Important Notes
Commands are case-sensitive; type them exactly as shown.
Use db.collectionName.find().pretty() to see results in a readable format.
Remember to switch to the right database before running commands.
Summary
mongosh is your tool to interact with MongoDB using commands.
Use show dbs, use, and show collections to explore your data.
Insert and find data easily with insertOne() and find().
Practice
1. What command in
mongosh lists all the databases available on the server?easy
Solution
Step 1: Understand the command purpose
The question asks for the command that lists all databases in mongosh.Step 2: Recall mongosh commands
show dbsis the correct command to list databases. Other options are incorrect or do not exist.Final Answer:
show dbs -> Option AQuick 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
Solution
Step 1: Identify the command to change database
In mongosh, the command to switch databases isusefollowed by the database name.Step 2: Match the correct syntax
use shopcorrectly switches to the 'shop' database. Other options are invalid commands.Final Answer:
use shop -> Option BQuick 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:
What will be the output of the
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
Solution
Step 1: Insert a document into 'products' collection
TheinsertOne()command adds a document with name 'Pen' and price 1.5 to the 'products' collection.Step 2: Query the collection for documents with name 'Pen'
Thefind()command searches for documents matching {name: 'Pen'}. Since we inserted one, it returns an array with that document including an auto-generated _id.Final Answer:
[{ _id: ObjectId("...") , name: 'Pen', price: 1.5 }] -> Option AQuick 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:
But get an error:
db.users.insertOne({name: 'Alice', age: 30})But get an error:
ReferenceError: db is not defined. What is the most likely cause?medium
Solution
Step 1: Understand the error message
ReferenceError: db is not definedmeans the shell does not know whatdbis.Step 2: Identify cause in mongosh context
This usually happens if you have not selected a database withuse. Without a current database,dbis undefined.Final Answer:
You forgot to switch to a database usingusecommand -> Option CQuick 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
Solution
Step 1: List collections in current database
show collectionslists all collections in the current database. This is the first correct step.Step 2: Insert document into 'orders' collection
Usedb.orders.insertOne(...)to insert a document into the 'orders' collection. This is the correct syntax.Final Answer:
show collections db.orders.insertOne({item: 'Book', qty: 3}) -> Option DQuick 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
