🗂️

Introduction to MongoDB

Jul 13, 2024

Introduction to MongoDB

Speaker

  • Name: Jesse Hall
  • Role: Senior Developer Advocate at MongoDB
  • Alias: Known on YouTube as Code Stacker

Agenda

  1. Document Database Basics
  2. Create a Free MongoDB Atlas Cluster
  3. Load Data into Cluster (from JSON file)
  4. Clone an E-commerce Project
  5. Connect the App to MongoDB Atlas

Key Concepts

Relational Databases (SQL)

  • Data is stored in rows and columns (like spreadsheets)
  • Related data is stored in separate tables to avoid duplication
  • Example: users table and phone numbers table
  • Requires joining tables to query related data

Document Databases (MongoDB)

  • Stores data in JSON documents
  • Stores data in key-value pairs; supports various data types (strings, numbers, booleans, arrays, nested objects)
  • Stores related data in the same document (no need for separate collections)
  • Key Principle: Data accessed together should be stored together -> Faster queries

Terminology

  • Database: Same in both relational and document databases
  • Table (SQL) = Collection (MongoDB)
  • Row (SQL) = Document (MongoDB)
  • Column (SQL) = Field (MongoDB)

Accessing Data in MongoDB

MongoDB Query API

  • Query and update documents using powerful operations ($updateMany, $set)
  • Indexing: Optimize queries with various index types (compound, multi-key, wildcard, geospatial, etc.)
  • Transformation & Analysis: Create real-time analytics, deploy full-text search, and perform graph operations
  • Join & Union: Use aggregation stages like $lookup and $unionWith

Query Examples

  • SQL: SELECT * FROM people
  • MongoDB: db.people.find({})
  • SQL: SELECT * FROM people WHERE age = 25
  • MongoDB: db.people.find({ age: 25 })
  • SQL: SELECT * FROM people WHERE age > 25
  • MongoDB: db.people.find({ age: { $gt: 25 } })*

Client Drivers

  • Available for various languages (e.g., C#, Go, Java, Node.js, Python, etc.)

Live Demo

Steps

  1. Create MongoDB Atlas Account
    • Option: Free forever tier
  2. Build a New Database
    • Types: Serverless, Dedicated, Shared (Free)
    • Provider: AWS, Google Cloud, or Azure
    • Create Cluster
  3. Set Up Access
    • Create user (e.g., username: admin, password: super-secret)
    • Add IP address to access list
  4. Clone E-commerce Repo
    • Load sample data (products.json)

Using MongoDB Compass

  • Purpose: GUI for MongoDB
  • Connection String: Get from MongoDB Atlas
  • Import Data: Upload JSON file (e.g., products.json)

Application Setup (VS Code)

  1. Clone Repository
  2. Switch to Specific Branch (quick-demo)
  3. Install Dependencies (npm install)
  4. Configure Environment Variables
    • File: .env.local with MongoDB URI
  5. Run Application (npm run dev)
  6. View on localhost:3000
  7. Fetch Products Data
    • Connects to MongoDB, pulls products, and displays them

MongoDB Connection in Code

  • File: lib/mongodb.js
  • Usage: Returns client promise for database operations

Server-Side Data Fetching (Next.js)

  • File: pages/index.js
  • Function: getServerSideProps
    • Connects to DB, fetches products collection, returns data to component

Testing Query Changes

  • Example: Fetch only products with category Hoodie
    • Modify find query with { category: 'Hoodie' }

Conclusion

  • MongoDB setup, data loading, connection to app, and data fetching demo