Looking Around in Compass

Connect Compass to mongodb://localhost:27017, find the sunday_kitchen database and its recipes in the sidebar, see the documents as a list, as JSON and as a table, filter them, fix a typo in one, and choose between Compass and mongosh.

Novice · MongoDB · Updated

By

After this lesson you can

  • Connect Compass to mongodb://localhost:27017 and find a database's collections and documents in its sidebar
  • Filter documents in Compass's Filter box and edit one document, and choose Compass or mongosh for a task

Key takeaways

  • A connection string such as mongodb://localhost:27017 says what to connect to, where (localhost, this computer) and at which port (27017).
  • Compass's sidebar lists the databases, and under each one its collections. A collection's Documents tab shows its documents.
  • The Documents tab shows them in List, JSON or Table view, 25 at a time. The Filter box takes the same braces as find, and Find runs it.
  • To edit a document, hover over it, click the pencil, change the value and click Update.
  • Compass suits looking around and fixing one thing; mongosh suits repeating, copying and saving commands, and the exercises.

Sometimes you want to see the data rather than ask for it: scan a whole collection for anything odd, show a teammate what a document looks like, or fix one wrong value without typing a command. Compass, the app you installed with MongoDB, is a window into the same databases the shell talks to.

Sunday Kitchen has a reason to look today. Maya's handwritten note says the Lemon Bars take 50 minutes; the 55 on the card, and in the database since Your First Documents in mongosh, was a typo. You'll find it and fix it in Compass.

Connect to your MongoDB

Open MongoDB Compass. In the Connections sidebar on the left, click Add New Connection. The New Connection window asks for a connection string, the one-line address of a MongoDB:

mongodb://localhost:27017

Read it in three parts: mongodb:// says what follows is a MongoDB address, localhost is this computer, and 27017 is the port MongoDB listens at, the same address the shell uses. Type it in if the box is empty, then click Save & Connect. (On Atlas, paste the string you copied from the cluster's Connect window instead, as Another Way: A Free Cluster on MongoDB Atlas shows.)

Databases and collections on the left

Once it's connected, the sidebar lists your connection with its databases under it: admin, config and local, which MongoDB keeps for itself, and sunday_kitchen. Click the arrow beside sunday_kitchen to see its collections, then click recipes.

The Documents tab opens with the three recipes: the same documents db.recipes.find() listed in the shell, each with its _id.

MongoDB Compass connected to localhost:27017, with sunday_kitchen open in the sidebar and its recipes collection chosen, and the Documents tab listing three recipes, Saturday Pancakes, Weeknight Chili and Lemon Bars, each with its _id, name, meal and minutes

The Documents tab has three View buttons that change how they're shown:

View Shows each document as Handy for
List View (the first one Compass shows) A card of fields and values Reading a few documents
JSON View The text of the document, braces and all Copying a document exactly
Table View A row, with a column for each field Comparing one field across many documents

Compass shows 25 documents at a time, with a menu under the query bar to show 50, 75 or 100 instead.

The Filter box

The Documents tab's query bar has a Filter box that takes the same braces you'd put inside find. Type a filter and click Find:

{ name: "Lemon Bars" }

Only Lemon Bars is left in the list, minutes: 55 and all. That's the filter from the shell, with Compass running it for you. Click Reset to clear the filter and see all three again.

Fix one document

With Lemon Bars showing, hover over its document and click the pencil icon (Edit document). The fields become editable. Click the value 55, change it to 50, and click Update to save the change (Cancel leaves it as it was). Compass highlights the field you changed in yellow until you save.

Compass's List View with the Lemon Bars document in edit mode: the minutes value changed to 50 and highlighted in yellow, and the Cancel and Update buttons below it

Compass and the shell share the same data, so the shell sees the fix at once. In VS Code's terminal, start mongosh, type use sunday_kitchen, then:

db.recipes.find({ name: "Lemon Bars" })
[
  {
    _id: ObjectId('6afc9a5efa6b0c5171e7a78b'),
    name: 'Lemon Bars',
    meal: 'dessert',
    minutes: 50
  }
]

Compass or mongosh?

Both reach the same documents. Pick by the job:

The job Reach for
Looking around a collection you don't know yet Compass
Fixing one value you can see Compass
Running the same command again, or many in a row mongosh
Saving a command to share, or keeping it in a file mongosh
Writing the answer to an exercise mongosh (the exercises check mongosh statements)

Most developers keep both open: Compass to look, the shell to do.

Check yourself

2 questions to try on the lesson page.

So what

Compass lets you see your data the way a person reads it, and fix it with the mouse; the shell lets you ask for it the way a program does. Knowing both means you can always check what's really there.

Key terms

Related