β‘ MongoDB Shell (mongosh) Installation
Complete guide to install the modern MongoDB Shell on Windows, macOS, and Linux
What is MongoDB Shell?
The modern JavaScript interface for MongoDB
mongosh is the modern, JavaScript-based MongoDB Shell that replaced the legacy mongo shell. It provides an enhanced command-line interface for interacting with MongoDB databases, with better syntax highlighting, improved autocomplete, and full JavaScript support.
- Old:
mongo(deprecated as of MongoDB 5.0) - New:
mongosh(modern, actively maintained) - Recommendation: Always use
mongoshfor new projects
Windows Installation
Two installation methods
Step 1: Download the Windows MSI installer:
Download mongosh for WindowsStep 2: Run the installer:
- Double-click the downloaded
.msifile - Click "Next" through the installation wizard
- Accept the license agreement
- Choose installation location (default is fine)
- Click "Install"
- Click "Finish" when complete
If you have Chocolatey installed, you can install mongosh with one command:
macOS Installation
Using Homebrew or manual install
The easiest way to install mongosh on macOS:
Step 1: Download the macOS package:
Download mongosh for macOSStep 2: Extract and install:
Linux Installation
Ubuntu/Debian and RHEL/CentOS
Verify Installation
Test mongosh is working
If you have MongoDB running locally, test the connection:
Basic Usage
Essential mongosh commands
mongosh supports modern JavaScript (ES6+):
Troubleshooting
Common issues and solutions
Symptoms: mongosh: command not found
Windows Solution:
- Restart your terminal/PowerShell
- Check if mongosh is in PATH:
where mongosh - Reinstall using the MSI installer
macOS/Linux Solution:
- Check if installed:
which mongosh - Verify it's in
/usr/local/bin - Add to PATH if needed:
$ echo 'export PATH="/usr/local/bin:$PATH"' >> ~/.zshrc $ source ~/.zshrc
Symptoms: MongoServerError: connect ECONNREFUSED
Solutions:
- Make sure MongoDB server is running
- Check MongoDB is listening on port 27017
- Verify connection string is correct
- Check firewall settings
Symptoms: Permission denied when running mongosh
Solution: