Introduction to MongoDB

1/10/2025

Introduction to MongoDB

MongoDB is a document-oriented NoSQL database used for high volume data storage.

What is MongoDB?

MongoDB stores data in flexible, JSON-like documents, meaning fields can vary from document to document.

Key Concepts

  • Database: Container for collections
  • Collection: Similar to a table (group of documents)
  • Document: A record (JSON-like object)
  • Field: A key-value pair in a document

Installation

bash
# Using Docker
docker run -d -p 27017:27017 --name mongodb mongo

# Connect with mongosh
mongosh "mongodb://localhost:27017"

Basic Commands

javascript
// Show databases
show dbs

// Use/create database
use myapp

// Show collections
show collections

// Create collection
db.createCollection('users')

// Insert document
db.users.insertOne({
  name: 'John',
  email: 'john@example.com',
  age: 30
})

// Find documents
db.users.find()
db.users.find({ name: 'John' })