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:
{
_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:
mongosh
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
db.version()
7.0.5
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.