0
0
MongoDBquery~5 mins

MongoDB Shell (mongosh) basics - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is mongosh in MongoDB?

mongosh is the interactive command-line shell for MongoDB. It lets you connect to your database, run commands, and see results immediately.

Click to reveal answer
beginner
How do you connect to a local MongoDB server using mongosh?

Simply type mongosh in your terminal and press Enter. It connects to mongodb://localhost:27017 by default.

Click to reveal answer
beginner
What command lists all databases in mongosh?

Use show dbs to see all databases on the connected MongoDB server.

Click to reveal answer
beginner
How do you switch to a specific database in mongosh?

Use use <databaseName>. For example, use mydb switches to the database named mydb.

Click to reveal answer
beginner
How do you insert a document into a collection using mongosh?

Use db.collectionName.insertOne({key: 'value'}). For example, db.users.insertOne({name: 'Alice', age: 25}) adds a user document.

Click to reveal answer
What does the command show dbs do in mongosh?
AConnects to a new database
BShows all collections in the current database
CDisplays documents in a collection
DLists all databases on the server
How do you switch to a database named shop in mongosh?
Ause shop
Bconnect shop
Cswitch shop
Ddb shop
Which command inserts a document into the products collection?
Adb.products.add({name: 'Pen'})
Bdb.products.insertOne({name: 'Pen'})
Cinsert db.products {name: 'Pen'}
Dadd products {name: 'Pen'}
What is the default address mongosh connects to if no parameters are given?
Amongodb://localhost:27017
Bmongodb://127.0.0.1:8080
Cmongodb://remotehost:27017
Dmongodb://localhost:3000
How do you see all collections in the current database?
Adb.showCollections()
Blist collections
Cshow collections
Dcollections show
Explain how to connect to MongoDB using mongosh and switch to a database named testdb.
Think about starting the shell and changing the database context.
You got /3 concepts.
    Describe the steps to insert a new document into a collection called customers using mongosh.
    Focus on the insertOne method and document format.
    You got /3 concepts.