Stop 05 · MongoDB FundamentalsTopics · 4/9

mongosh and MongoDB Compass

Part of the MERN Stack Roadmap

In short

Before writing Mongoose code, you need to see what's actually in your database. mongosh lets you type queries quickly, and Compass lets you browse, filter and check indexes with a mouse.

mongosh is the MongoDB command-line shell for running queries, and Compass is the desktop app for browsing collections, building queries and viewing indexes visually.

The jaadu stands on a warehouse balcony with binoculars around the neck, talking into a walkie-talkie while looking over rows of shelves below.

Think of it like this

The walkie-talkie and the binoculars

A warehouse manager uses a walkie-talkie to give quick, exact orders (mongosh) and binoculars from the gallery to look over every aisle at once (Compass). Same warehouse, two ways to work with it.

Watch it happen

Shell or GUI?

mongosh

  • Command-line shell, runs JavaScript
  • Fast for quick queries and scripts
  • Same syntax as the Node driver, mostly
  • Great on servers with no screen

MongoDB Compass

  • Free desktop app
  • Browse documents as a table or JSON
  • Visual query, aggregation and index tools
  • Shows schema and explain plans

Use both. Compass to explore and understand, mongosh to test exact queries you will move into code.

The big ideas

What you need to know

Basic shell commands

show dbs, use shopdb, show collections, then db.products.find(). The use command switches databases.

It's JavaScript

mongosh runs JavaScript, so you can use variables, loops and functions to insert test data.

Compass views

Documents, Aggregations, Schema, Explain Plan and Indexes tabs show your data from different angles.

Same connection string

Both tools connect with the same mongodb+srv URI your app uses.

See it in code

Walk through the code

js
1// mongosh2show dbs3use shopdb4show collections5 6db.products.insertMany([7  { name: "Steel Bottle", price: 450, category: "kitchen" },8  { name: "Notebook", price: 60, category: "stationery" },9]);10 11db.products.find({ category: "kitchen" })12db.products.countDocuments()13db.products.findOne({ name: "Notebook" })

1/4List databases, switch to shopdb and list its collections.

A quick session in mongosh. Open Compass afterwards and you'll see the same two documents.

Save yourself the pain

Common mistakes

Oops

Running queries in the wrong database without noticing.

Do this instead

Run db.getName() or check the prompt. use shopdb before working.

Oops

Editing production data by hand in Compass casually.

Do this instead

Use a separate dev database or cluster. Treat production edits like code changes.

Your mission

Explore with both tools

~20 min

Quick check

In mongosh, what does use shopdb do if shopdb doesn't exist yet?

Go deeper

  1. 1Connect to your Atlas cluster from both mongosh and Compass.
  2. 2Use show dbs, use and show collections to look around.
  3. 3Run a query in Compass, then copy the same filter into mongosh.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge