MongoDB Database
NoSQL document database for flexible data models
Overview
MongoDB is a powerful NoSQL database that stores data in flexible, JSON-like documents.
Features
- Flexible Schema: Adapt to changing requirements
- Scalable: Horizontal scaling built-in
- Rich Queries: Complex query support
- Aggregation: Powerful data processing
Connection
import { MongoClient } from 'mongodb';
const client = new MongoClient(process.env.MONGODB_URL);
await client.connect();
const db = client.db('myapp');CRUD Operations
// Insert
await db.collection('users').insertOne({ name: 'John', email: 'john@example.com' });
// Find
const users = await db.collection('users').find({ active: true }).toArray();
// Update
await db.collection('users').updateOne(
{ _id: userId },
{ $set: { lastLogin: new Date() } }
);
// Delete
await db.collection('users').deleteOne({ _id: userId });