๐ŸŸข Getting Started with MongoDB Shell (mongosh): A Modern CLI for MongoDB

If you’re working with MongoDB — one of the world’s most popular NoSQL databases — chances are you’ll need a command-line interface (CLI) to interact with your data. While earlier versions of MongoDB came with a shell called mongo, today, MongoDB recommends using mongosh, the modern MongoDB Shell.

In this post, we’ll introduce mongosh, explain why it replaced the legacy shell, show you how to install and use it, and walk through some essential commands. Whether you're new to MongoDB or just transitioning to mongosh, this guide will help you get comfortable with the basics.


๐Ÿ“Œ What Is mongosh?

mongosh (short for MongoDB Shell) is the official, interactive shell for MongoDB. It allows you to connect to MongoDB instances, run queries, insert or update documents, manage databases, and perform administrative tasks — all from your terminal.

Unlike the old mongo shell, which was built in C++, mongosh is written in Node.js and offers a modern developer experience, complete with:

  • Syntax highlighting

  • Command autocompletion

  • Better error messages

  • JavaScript and Node.js API support

It's designed to make working with MongoDB easier, especially for developers familiar with JavaScript.


๐Ÿ”„ Why Did MongoDB Replace the Legacy Shell?

The older mongo shell served developers for many years but lacked modern features like improved scripting, extensibility, and a rich interactive experience.

Here’s a quick comparison:

Featuremongo (Legacy)mongosh (New)
Built withC++Node.js
Autocompletion
Syntax Highlighting
JavaScript SupportLimitedFull ECMAScript
Plugin SupportPlanned
Active DevelopmentDeprecated

In short, mongosh is built for modern workflows and aligns better with JavaScript ecosystems and developer expectations.


⚙️ How to Install mongosh

๐Ÿ’ป On Windows

  1. Go to MongoDB Shell Downloads

  2. Choose your OS and version.

  3. Install and optionally add mongosh to your system PATH.

๐Ÿ On macOS (Homebrew)

bash

brew tap mongodb/brew brew install mongosh

๐Ÿง On Ubuntu/Debian

bash

sudo apt-get install gnupg wget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc | sudo apt-key add - echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/6.0 multiverse" \ | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list sudo apt-get update sudo apt-get install -y mongodb-mongosh

To verify installation:

bash

mongosh --version

๐Ÿ”Œ Connecting to MongoDB with mongosh

Once installed, start the shell by running:

bash

mongosh

This connects to your local MongoDB instance (mongodb://localhost:27017) by default.

To connect to a remote MongoDB Atlas cluster:

bash

mongosh "mongodb+srv://<username>:<password>@<cluster-url>/test"

Replace <username>, <password>, and <cluster-url> with your actual credentials.


๐Ÿงช Basic mongosh Commands

Here are some useful commands to get started:

๐Ÿ“‚ Database & Collection Navigation

js

show dbs // Lists all databases use myDatabase // Switches to or creates a database show collections // Lists collections in current DB

๐Ÿ“„ Working with Documents

js

// Insert db.users.insertOne({ name: "Alice", age: 30 }); // Find db.users.find({ age: { $gt: 25 } }); // Update db.users.updateOne({ name: "Alice" }, { $set: { age: 31 } }); // Delete db.users.deleteOne({ name: "Alice" });

๐Ÿ“œ JavaScript in mongosh

mongosh supports ES6 syntax. You can declare variables, write functions, and use loops:

js

let result = db.users.find({ active: true }); result.forEach(doc => console.log(doc.name));

This JavaScript-native environment makes scripting and automation much smoother than in the old shell.


๐ŸŒŸ Notable Features in mongosh

✅ Autocompletion

Hit the Tab key to auto-complete collection names, methods, and field names — a huge time-saver.

✅ Syntax Highlighting

Commands are color-coded to improve readability and reduce syntax errors.

✅ Rich Error Messages

Errors in mongosh are more descriptive and beginner-friendly.

✅ Integration with MongoDB Compass

You can use the same shell inside MongoDB Compass, the official GUI tool — no context switching required.


๐Ÿงพ Final Thoughts

The MongoDB Shell has come a long way — from the basic, no-frills mongo shell to the modern, JavaScript-powered mongosh. If you're just starting out with MongoDB or looking to upgrade your development workflow, mongosh is the tool you want.

It’s powerful, intuitive, and perfectly tailored for developers who live and breathe JavaScript.

๐Ÿ’ก Pro Tip: Practice using mongosh alongside MongoDB Compass and Atlas to get the best of both CLI and GUI worlds.


๐Ÿ”— Useful Resources


Comments

Popular posts from this blog

Query Operator's

Creating Documents in MongoDB(Insert)