How to Connect and Use Mongosh in VS Code via MongoDB Extension
Last updated
Last updated
You can use MongoDB for VS Code to connect to MongoDB and interact with it using mongosh. Here’s a step-by-step guide:
Open VS Code.
Click on the Extensions tab (or press Ctrl+Shift+X
).
In the search bar, type "MongoDB for VS Code".
Click Install on the MongoDB for VS Code extension.
Click on the MongoDB icon on the Activity Bar (left sidebar in VS Code).
Click "Connect".
If you have a local MongoDB server that provide daemon service, click localhost:27017 with right mouse and select connect>
Get MongoDB Server service on (start service) and then run mongod at command terminal.
If you have a MongoDB Atlas cloud database, follow these steps:
Go to .
Select your database and click "Connect".
Choose "Connect using MongoDB Compass" and copy the connection string.
Paste the connection string in VS Code’s MongoDB extension.
Replace <password>
with your database password.
Open a new VS Code terminal (Ctrl+
`
on Windows/Linux or Cmd+
`
on macOS).
Type:
This will connect to your default MongoDB instance. Select Launch MongoDB Shell to open mongosh shell at the vscode terminal.
Show all databases:
Switch to a database:
Insert a document:
Retrieve documents:
Update a document:
Delete a document:
Click "New MongoDB Playground" in the MongoDB extension.
Write MongoDB queries and click "Run All" to execute them.
To exit the shell, type:
To disconnect from MongoDB in VS Code, click the disconnect icon in the MongoDB extension.
Now you can connect, query, and manage MongoDB databases inside VS Code using mongosh
! 🚀