KevsRobots Learning Platform
By Kevin McAleer, 6 Minutes
Page last updated October 18, 2026

You have a server running and a prompt blinking at you. In this lesson we will look around, create the workshop database, and store our first part in it. By the end you will have used the handful of commands you will type every single time you open mongosh.
If your shell isn’t open, start it the same way as last lesson (Docker users: docker exec -it mongodb mongosh).
The prompt looks like this:
test>
The word before the > is the database you are currently using. mongosh always starts in a database called test. You type a command after the >, press Enter, and the result appears underneath.
mongosh speaks JavaScript, the language of web pages. You don’t need to know JavaScript for this course, but a few patterns will come up again and again:
db.parts.find() - the dots go from big to small: the current database (db), then the parts collection, then the find action() at the end mean “do it now”. Leave them off and nothing useful happens{ } is a document, written just like the ones in lesson 02"servo". Numbers don’t: 12Let’s see what databases the server already has:
show dbs
admin 40.00 KiB
config 12.00 KiB
local 40.00 KiB
These three are MongoDB’s own housekeeping databases. Leave them alone. Your sizes may be slightly different, and Atlas users may see a different list.
To switch to a database, use use:
use workshop
switched to db workshop
The prompt changes to workshop>. You can always check which database you are in by typing db on its own:
db
workshop
Now let’s look at the list again:
show dbs
admin 40.00 KiB
config 12.00 KiB
local 40.00 KiB
Where has it gone? This catches everyone out. MongoDB creates databases lazily, meaning it doesn’t actually make the database until you put something in it. use workshop just says “I’d like to work in a database called workshop”. There are no collections yet either:
show collections
This prints nothing at all, which is MongoDB’s way of saying “empty”.
Let’s put a servo in the drawer. insertOne adds one document to a collection:
db.parts.insertOne({ name: "SG90 Micro Servo", category: "servo", quantity: 12 })
{
acknowledged: true,
insertedId: ObjectId('6ad5238e22220cc4e52f6769')
}
Two things just happened. The parts collection didn’t exist, so MongoDB created it. And because the collection now has data in it, the workshop database was created too. The reply tells us:
acknowledged: true - the server received and saved the documentinsertedId - the unique _id MongoDB gave our servo. Yours will be a different jumble of letters and numbersNow both show up:
show collections
parts
show dbs
admin 40.00 KiB
config 12.00 KiB
local 40.00 KiB
workshop 8.00 KiB
find() with empty brackets returns every document in the collection:
db.parts.find()
[
{
_id: ObjectId('6ad5238e22220cc4e52f6769'),
name: 'SG90 Micro Servo',
category: 'servo',
quantity: 12
}
]
There is our servo, with the _id field MongoDB added for us. The square brackets around it mean find() gives back a list, which just happens to have one item in it for now.
To count documents rather than show them, use countDocuments():
db.parts.countDocuments()
1
Let’s add a second part, then count again:
db.parts.insertOne({ name: "HC-SR04 Ultrasonic Sensor", category: "sensor", quantity: 6 })
{
acknowledged: true,
insertedId: ObjectId('6ad5238e22220cc4e52f676a')
}
db.parts.countDocuments()
2
A few things that will save you a lot of typing:
| Key or command | What it does |
|---|---|
| Up arrow | Bring back the previous command so you can run or edit it |
| Tab | Auto-complete. Type db.parts.countD and press Tab to get countDocuments |
cls |
Clear the screen |
help |
List the shell’s built-in commands |
exit |
Leave mongosh (quit works too) |
When you exit and come back later, remember that you start in test again. Type use workshop first, and your parts will be right where you left them.
insertOne, then check the count is now 3.use test, then db.parts.countDocuments(). Why is the answer different? Switch back with use workshop.db.parts.find() again and find the _id of your new part.Don’t worry about tidying up these test parts. Next lesson starts by clearing the collection and loading the full Parts Bin.
Problem: You type use workshop but show dbs doesn’t list it.
Solution: Insert a document first.
Why: Databases and collections are only created when the first document is written.
Problem: db.Parts.find() returns nothing, even though you just added parts.
Solution: Use the exact name, db.parts.find().
Why: Collection names are case sensitive. Parts and parts are two different collections.
Problem: TypeError: db.parts.countDocument is not a function
Solution: Check the spelling (it is countDocuments, with an “s”) or use Tab to auto-complete.
Why: The shell only knows the exact names of its functions.
Problem: Typing db.parts.find prints a wall of text about a Function.
Solution: Add the brackets: db.parts.find().
Why: Without brackets you are asking the shell to describe the function rather than run it.
db prints it and use switches itshow dbs and show collections list what exists, and databases appear only once they hold datainsertOne adds a document, find() shows them and countDocuments() counts themcls, help and exit make the shell much friendlierNext, we will look properly at inserting documents and load the full Parts Bin.
You can use the arrows ← → on your keyboard to navigate between lessons.
Comments