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).
Cluster → Databases → Collections → Documents. Database ≈ database in SQL. Collection ≈ table in SQL (but schemaless by default). Document ≈ row in SQL (but with nested structure).
mongosh commands you'll use daily.
// Connect, Atlas connection string or localmongosh "mongodb+srv://user:[email protected]"// Server infotest> show dbstest> use myapp // switch to (or create) database 'myapp'myapp> show collections// Create a docmyapp> db.users.insertOne({name: "Alex",age: 30,tags: ["admin", "beta"],address: { city: "NYC", zip: "10001" },createdAt: new Date()})// Readmyapp> db.users.find()myapp> db.users.find().pretty()myapp> db.users.findOne({ name: "Alex" })// Delete the test dbmyapp> db.dropDatabase()
BSON adds types JSON doesn't have.
{_id: ObjectId("507f1f77bcf86cd799439011"), // Mongo's default ID type (12 bytes)age: 30, // Int32views: NumberLong("9999999999"), // Int64 (Long)balance: NumberDecimal("49.99"), // Decimal128, use for moneyactive: true, // Booleantags: ["a", "b"], // Arraynested: { foo: 1 }, // Embedded documentcreatedAt: ISODate("2026-01-01T00:00:00Z"), // Datepic: BinData(0, "base64payload"), // Binaryref: DBRef("users", ObjectId("...")), // Reference (rare)pos: { type: "Point", coordinates: [-73, 40] } // GeoJSON}
Mongo indexes look familiar.
db.users.createIndex({ email: 1 }, { unique: true });db.users.createIndex({ "address.city": 1, age: -1 }); // compounddb.users.getIndexes();
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.