0
0
MongoDBquery~10 mins

MongoDB Shell (mongosh) basics - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - MongoDB Shell (mongosh) basics
Start mongosh
Connect to MongoDB
Show databases
Select database
Show collections
Run queries or commands
Exit mongosh
This flow shows how you start the MongoDB shell, connect to the server, explore databases and collections, run commands, and then exit.
Execution Sample
MongoDB
mongosh
show dbs
db = db.getSiblingDB('testdb')
db.createCollection('users')
db.users.insertOne({name: 'Alice'})
db.users.find()
This code starts the shell, lists databases, switches to 'testdb', creates a collection, inserts a document, and finds documents.
Execution Table
StepCommandActionResult
1mongoshStart MongoDB shellConnected to MongoDB server
2show dbsList all databases[admin, local, testdb]
3db = db.getSiblingDB('testdb')Switch to 'testdb' databaseSwitched to 'testdb'
4db.createCollection('users')Create 'users' collectionCollection 'users' created
5db.users.insertOne({name: 'Alice'})Insert document into 'users'Inserted 1 document with _id
6db.users.find()Find all documents in 'users'[{_id: ObjectId(...), name: 'Alice'}]
7exitExit mongoshShell closed
💡 User exits the shell with 'exit' command
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 5After Step 6Final
dbdefaulttestdbtestdbtestdbtestdbtestdb
users collectionnonenonecreatedcreated with 1 documentcreated with 1 documentcreated with 1 document
Key Moments - 3 Insights
Why do we use db.getSiblingDB('testdb') instead of just db?
Because 'db' starts as the default database (usually 'test'), getSiblingDB switches to the named database without restarting the shell. See execution_table step 3.
What does insertOne return and why is _id important?
insertOne returns an object with the inserted document's _id, which uniquely identifies the document. This is shown in execution_table step 5.
Why does find() return an array with ObjectId?
Because find() returns documents matching the query; each document has a unique _id of type ObjectId. See execution_table step 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'db' after step 3?
A'testdb'
B'admin'
C'local'
D'default'
💡 Hint
Check the variable_tracker table under 'db' after Step 3
At which step is the 'users' collection created?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look at the 'Action' column in execution_table for collection creation
If you run db.users.find() before inserting any documents, what would the result be?
AA document with _id
BAn error
CAn empty array []
DThe collection is deleted
💡 Hint
Think about what find() returns when no documents exist in the collection
Concept Snapshot
MongoDB Shell (mongosh) basics:
- Start shell with 'mongosh'
- Use 'show dbs' to list databases
- Switch DB with db.getSiblingDB('name')
- Create collections with createCollection()
- Insert documents with insertOne()
- Query documents with find()
- Exit shell with 'exit'
Full Transcript
This lesson shows how to use the MongoDB shell called mongosh. First, you start the shell by typing 'mongosh'. Then, you can list all databases using 'show dbs'. To work with a specific database, use db.getSiblingDB('databaseName'). You can create a collection with createCollection('collectionName'). To add data, use insertOne() with a document. To see data, use find(). Finally, exit the shell by typing 'exit'. Each step changes the state of the shell and database, which we tracked in the tables.