R
Rishtaara
MongoDB Fundamentals
Lesson 7 of 40Article14 min

Database, Collection & Document

A database is a container for collections — similar to a SQL database name. A collection groups related documents — like a table but without enforced identical columns. A document is one BSON record with fields and values.

Terminology — database, collection, document

A database is a container for collections — similar to a SQL database name. A collection groups related documents — like a table but without enforced identical columns. A document is one BSON record with fields and values.

MongoDB creates databases and collections lazily: the first write to db.myCollection.insertOne(...) creates them if they did not exist.

Real-life example: Database = school building. Collection = classroom. Document = one student's report card. Different students can have different optional sections on their cards.

  • Database — namespace boundary (permissions, backups often scoped here)
  • Collection — set of documents (naming: lowercase, plural is convention, e.g. users)
  • Document — BSON object with _id; max size 16 MB
  • Field — key: value inside a document (dot notation for nested: address.city)

createCollection, show dbs, and use

use rishtaara switches the shell context to the rishtaara database (creates it on first write). show dbs lists database names. show collections lists collections in the current database.

createCollection is optional — inserts create collections automatically — but explicit creation lets you set options like capped collections or validators.

Real-life example: use db is walking into a specific office wing. show dbs is the building directory. createCollection is labeling a new filing cabinet before anyone drops files in.

Tip: admin and local are system databases — avoid storing app data in admin. Your app database might be rishtaara, ecommerce, or testdb.
Navigate and create collections
show dbs

use rishtaara
db.getName()              // "rishtaara"

db.createCollection("courses")
show collections

// Implicit create on first insert
db.students.insertOne({ name: "Priya", grade: 11 })
show collections          // students appears

Sample document structure

Documents mirror JSON objects: strings in double quotes in strict JSON, but mongosh accepts unquoted keys. Arrays hold lists; embedded objects represent nested one-to-one or one-to-few data.

Every document needs _id — MongoDB generates ObjectId if you omit it.

Real-life example: A user document is a contact card — name at the top, phone list in the middle, address block at the bottom. One card, many field types.

Sample user document
{
  _id: ObjectId("665a1b2c3d4e5f6789012345"),
  name: "Asha Kumar",
  email: "asha@example.com",
  roles: ["student", "editor"],
  profile: {
    city: "Mumbai",
    bio: "Learning MongoDB on Rishtaara"
  },
  enrolledCourses: [
    { slug: "mongodb-fundamentals", progress: 12 },
    { slug: "nodejs-basics", progress: 45 }
  ],
  active: true,
  createdAt: ISODate("2024-06-01T00:00:00Z")
}
Insert and read the document
db.users.insertOne({
  name: "Asha Kumar",
  email: "asha@example.com",
  roles: ["student"],
  active: true,
  createdAt: new Date()
})

db.users.findOne({ email: "asha@example.com" })