█
LastWrite
  • > Curriculum
  • > Pricing
  • > For Educators
  • > About
  • > Contact
Log InGet Started

Questions, concerns, bug reports, or suggestions? We read every message, write to us at [email protected].

More ways to reach us →
LastWrite

Structured computer science lessons for aspiring developers and security professionals.

[email protected]

(201) 785-7951

Mon–Fri, 9 AM–5 PM EST

Learn

  • Curriculum
  • Pricing

Company

  • About
  • For Educators & Schools
  • Contact Us

Legal

  • Terms of Service
  • Privacy Policy
© 2026 LastWrite. All rights reserved.
Curriculum/Programming Languages/MongoDB/MongoDB Fundamentals
35 minBeginner

MongoDB Fundamentals

After this lesson, you will be able to: Use the MongoDB shell: databases, collections, documents, BSON types.

Mongo's data model is simple once you see it. Databases hold collections; collections hold documents; documents are BSON (binary JSON).

Prerequisites:Document Databases vs Relational

The hierarchy

Cluster → Databases → Collections → Documents. Database ≈ database in SQL. Collection ≈ table in SQL (but schemaless by default). Document ≈ row in SQL (but with nested structure).

Shell basics

mongosh commands you'll use daily.

tsx
// Connect, Atlas connection string or local
mongosh "mongodb+srv://user:[email protected]"
// Server info
test> show dbs
test> use myapp // switch to (or create) database 'myapp'
myapp> show collections
// Create a doc
myapp> db.users.insertOne({
name: "Alex",
email: "[email protected]",
age: 30,
tags: ["admin", "beta"],
address: { city: "NYC", zip: "10001" },
createdAt: new Date()
})
// Read
myapp> db.users.find()
myapp> db.users.find().pretty()
myapp> db.users.findOne({ name: "Alex" })
// Delete the test db
myapp> db.dropDatabase()

BSON types — beyond JSON

BSON adds types JSON doesn't have.

tsx
{
_id: ObjectId("507f1f77bcf86cd799439011"), // Mongo's default ID type (12 bytes)
email: "[email protected]", // String
age: 30, // Int32
views: NumberLong("9999999999"), // Int64 (Long)
balance: NumberDecimal("49.99"), // Decimal128, use for money
active: true, // Boolean
tags: ["a", "b"], // Array
nested: { foo: 1 }, // Embedded document
createdAt: ISODate("2026-01-01T00:00:00Z"), // Date
pic: BinData(0, "base64payload"), // Binary
ref: DBRef("users", ObjectId("...")), // Reference (rare)
pos: { type: "Point", coordinates: [-73, 40] } // GeoJSON
}

💡 _id is mandatory

Every document has an `_id` field. If you don't supply one, Mongo generates an ObjectId. ObjectId encodes a timestamp + machine + counter, sortable + unique across nodes. You CAN use your own _id (e.g., email or UUID) but then YOU must guarantee uniqueness.

Indexes (preview — full lesson later)

Mongo indexes look familiar.

tsx
db.users.createIndex({ email: 1 }, { unique: true });
db.users.createIndex({ "address.city": 1, age: -1 }); // compound
db.users.getIndexes();

Common mistakes

Forgetting BSON has types JSON doesn't, storing money as plain number (Double) → rounding bugs. Treating _id as random ID when it encodes a timestamp, useful for sort-by-creation. Forgetting unique index on email → duplicates pile up. Inserting unbounded nested arrays (e.g., 'all messages in conversation'), doc size limit is 16 MB.

Sign in and purchase access to unlock this lesson.

Sign in to purchase
←Document vs Relational Databases
Back to MongoDB
CRUD Operations→