MongoDB Shell (mongosh) basics - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When using the MongoDB shell, commands run to fetch or change data. Understanding how long these commands take helps us work faster and smarter.
We want to know how the time to run a command changes as the amount of data grows.
Analyze the time complexity of the following MongoDB shell command.
db.users.find({ age: { $gt: 25 } })
This command looks for all users older than 25 in the users collection.
Look at what repeats when this command runs.
- Primary operation: Checking each user document to see if their age is greater than 25.
- How many times: Once for each user in the collection.
As the number of users grows, the command checks more documents.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The number of checks grows directly with the number of users.
Time Complexity: O(n)
This means the time to run the command grows in a straight line as the number of users grows.
[X] Wrong: "The command always runs instantly no matter how many users there are."
[OK] Correct: The command checks each user one by one, so more users mean more work and more time.
Knowing how commands scale with data size shows you understand how databases work behind the scenes. This skill helps you write better queries and explain your thinking clearly.
"What if we add an index on the age field? How would the time complexity change?"
Practice
mongosh lists all the databases available on the server?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]
- Confusing 'show collections' with listing databases
- Using non-existent commands like 'list databases'
- Trying 'dbs list' which is invalid
shop in mongosh?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]
- Using 'switch' instead of 'use'
- Adding extra words like 'db' or 'change'
- Typing commands that don't exist 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?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]
- Expecting find() to return null if document exists
- Confusing syntax causing errors
- Thinking find() returns a single object, not an array
db.users.insertOne({name: 'Alice', age: 30})But get an error:
ReferenceError: db is not defined. What is the most likely cause?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]
- Assuming insertOne method is missing
- Thinking syntax is wrong when it is correct
- Ignoring the need to start MongoDB server
orders. Which sequence of commands in mongosh is correct?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]
- Trying to 'use' a collection as a database
- Calling insertOne directly on db without collection
- Listing databases instead of collections
