Introduction

MongoDB stores data as flexible, JSON-shaped documents instead of rows in rigid tables — a different model from the relational databases this site's SQL course covers.

Documents instead of rows

A relational database like the one in this site's SQL course stores data as rows in tables, each row having exactly the columns the table defines. MongoDB stores documents — JSON-like objects with keys and values — grouped into collections instead of tables. Two documents in the same collection don't have to share the same fields:

JS a document
{
  _id: ObjectId("65f1a2b3c4d5e6f7a8b9c0d1"),
  name: "Priya",
  age: 29,
  tags: ["admin", "beta-tester"]
}

That's a document you might find in a users collection. Another document in the same collection could have completely different fields — MongoDB doesn't enforce a schema by default.

Connecting with mongosh

mongosh is MongoDB's official command-line shell — the equivalent of connecting to a SQL database with a client and typing queries directly. After installing MongoDB and starting the mongod server process, connecting looks like this:

bash terminal
mongosh
Output
Current Mongosh Log ID: 65f1a29e4c1a2b3c4d5e6f70
Connecting to:          mongodb://127.0.0.1:27017/?directConnection=true
Using MongoDB:          7.0.5
Using Mongosh:          2.1.1

test>

The prompt test> means you're connected and sitting in the default test database, ready to run commands.

A first command

JS mongosh
db.version()
Output
7.0.5
Note: MongoDB's schema flexibility is a genuine feature — you can evolve what a document looks like without a migration — but it's also a trap if you're not deliberate. Nothing stops you from accidentally inserting a document with a typo'd field name (naem instead of name) that silently becomes a whole new field instead of an error. Application-level validation matters more here than it does with a strict SQL schema.