MongoDB Shell and Compass

MongoDB offers two main ways to talk to a database. The first is mongosh, a text window where you type commands. The second is MongoDB Compass, a visual app where you click buttons and see your data in tables and charts. Beginners can pick either tool, and most professionals use both.

Think of mongosh as a chat window with your database. You type a command, press Enter, and the database sends back a reply. The name stands for MongoDB Shell. It runs on Windows, macOS, and Linux, and it understands JavaScript, so you can write variables, loops, and small functions right inside it.

How the Shell Talks to the Server

+--------------+    command     +-----------------+    reads/writes    +-------------+
|   mongosh    | ------------> |  mongod server  | -----------------> |  Data files |
| (you type)   | <------------ |  (port 27017)   | <----------------- |  on disk    |
+--------------+     reply     +-----------------+                    +-------------+

The shell never stores data itself. It sends your request to the mongod server, which does the real work and returns the answer.

Start the Shell

Open a terminal and type the following command to connect to a MongoDB server running on your own computer:

mongosh

Add a connection string to reach a server on another machine:

mongosh "mongodb://localhost:27017"

Essential Shell Commands

CommandWhat It Does
show dbsLists all databases that contain data
use shopSwitches to the database named shop
dbPrints the name of the current database
show collectionsLists the collections in the current database
helpDisplays a list of available commands
clsClears the screen
exitCloses the shell

Run Your First Commands

Type these three lines one by one. The first line selects a database called shop. The second line adds a product. The third line shows every product stored so far.

use shop
db.products.insertOne({ name: "Pen", price: 5 })
db.products.find()

The shop database did not exist before this moment. MongoDB creates a database and a collection the first time you save data into them.

Use JavaScript Inside the Shell

Because mongosh understands JavaScript, you can save time with small scripts. This loop adds three products at once:

const items = ["Pencil", "Eraser", "Ruler"];
items.forEach(function (item) {
  db.products.insertOne({ name: item, price: 10 });
});

What Is MongoDB Compass

Compass is the free graphical tool from MongoDB. It shows databases, collections, and documents in a window that looks like a regular desktop program. Anyone who prefers clicking over typing will feel at home here.

Compass Window Layout

+---------------------------------------------------------------+
| Connections |  shop  >  products                              |
+-------------+-------------------------------------------------+
| shop        | [Documents] [Aggregations] [Schema] [Indexes]   |
|   products  |-------------------------------------------------|
|   orders    | Filter: { price: { $gt: 5 } }       [Find]      |
| admin       |-------------------------------------------------|
| local       | { _id: 1, name: "Pen",    price: 5  }           |
|             | { _id: 2, name: "Pencil", price: 10 }           |
+-------------+-------------------------------------------------+

The left panel lists your databases and collections. The top tabs switch between different views of the same collection.

Connect with Compass

  1. Open Compass and find the connection box on the home screen.
  2. Paste the connection string, such as mongodb://localhost:27017.
  3. Click Connect.
  4. Pick a database from the left panel to see its collections.

Useful Compass Tabs

  • Documents shows every document and lets you filter, edit, insert, and delete.
  • Schema analyzes a sample of documents and displays the shape of each field with simple charts.
  • Explain Plan reveals whether a query uses an index.
  • Indexes lists existing indexes and creates new ones.
  • Aggregations builds pipelines stage by stage and previews the output of each stage.
  • Validation sets rules that every new document must follow.

Shell vs Compass

FeaturemongoshCompass
StyleText commandsPoint and click
Best forScripts, automation, servers without a screenExploring data, learning, quick edits
Learning curveModerateGentle
Speed for repeated tasksFast with saved scriptsSlower for bulk work

Tips for Beginners

  • Run commands in mongosh to learn the exact syntax that applications use.
  • Open the same collection in Compass to confirm what the command changed.
  • Press the Up arrow in mongosh to bring back the previous command and edit it.
  • Press the Tab key to auto-complete command and collection names.
  • Test every new query on sample data before using it on real data.

Summary

The mongosh shell gives full control through typed commands and JavaScript. Compass gives a visual window into the same data. Both tools connect to the same mongod server, so changes made in one tool appear instantly in the other. Learning both tools builds speed in typing and clarity in viewing.

Leave a Comment

Your email address will not be published. Required fields are marked *