MongoDB

MongoDB is a document-oriented NoSQL database that stores data as JSON-like documents.

Core Concepts

  • Database → Database

  • Collection → Table

  • Document → Row

  • Field → Column

CRUD Operations

// Connect
mongosh "mongodb://localhost:27017/innozverse"

// Insert
db.products.insertOne({
    name: "Surface Pro 12",
    category: "laptop",
    price: 999.99,
    specs: { ram: "16GB", storage: "512GB", cpu: "Snapdragon X" },
    tags: ["surface", "microsoft", "laptop"],
    stock: 50
})

db.products.insertMany([{...}, {...}])

// Find
db.products.find()
db.products.find({ category: "laptop" })
db.products.find({ price: { $gt: 500, $lt: 2000 } })
db.products.find({ tags: "surface" })
db.products.find({ "specs.ram": "16GB" })
db.products.findOne({ _id: ObjectId("...") })

// Projection (select specific fields)
db.products.find({}, { name: 1, price: 1, _id: 0 })

// Update
db.products.updateOne(
    { name: "Surface Pro 12" },
    { $set: { price: 899.99 }, $inc: { stock: -1 } }
)
db.products.updateMany(
    { category: "laptop" },
    { $set: { onSale: true } }
)

// Delete
db.products.deleteOne({ _id: ObjectId("...") })
db.products.deleteMany({ stock: 0 })

// Sort, limit, skip
db.products.find().sort({ price: -1 }).limit(10).skip(20)

Aggregation Pipeline

Indexes in MongoDB

Last updated