MongoDB Guide

MongoDBNoSQL Document Database

Learn MongoDB documents, collections, queries, aggregation pipeline, and building scalable NoSQL applications.

Contents

MongoDB Basics

MongoDB is a NoSQL document database. Data is stored as documents (JSON-like BSON) in collections.

javascript
// Connect to MongoDB
const { MongoClient } = require('mongodb');
const uri = "mongodb://localhost:27017";
const client = new MongoClient(uri);

async function main() {
  await client.connect();
  const db = client.db("mydb");
  const collection = db.collection("users");
  
  // Insert Document
  const result = await collection.insertOne({
    name: "John Doe",
    email: "john@example.com",
    age: 30
  });
  
  await client.close();
}

// Documents (BSON)
{
  _id: ObjectId("..."),
  name: "John Doe",
  email: "john@example.com",
  age: 30,
  tags: ["developer", "nodejs"],
  address: {
    street: "123 Main St",
    city: "New York"
  }
}

// Collections and Databases
// Database -> Collections -> Documents