MongoDB
Subject: nodejs
MongoDB
MongoDB is a leading NoSQL (Not Only SQL), document-oriented database. Unlike traditional relational databases, it stores data in flexible, JSON-like documents. This schema-less approach makes it highly agile and suitable for modern applications that need to handle diverse or rapidly changing data structures with high scalability and performance.
Why MongoDB? / Key Features
- Flexible Schema: Stores data in JSON-like documents, allowing different structures within the same collection. This supports rapid iteration and evolving data needs.
- Scalability: Designed for horizontal scaling through sharding, distributing data across many servers to handle vast amounts of data and traffic.
- High Availability: Achieves continuous operation and data redundancy using replica sets, groups of database instances with automatic failover.
- Performance: Optimized for speed with efficient indexing and an in-memory storage engine.
Core Concepts
- Document: The basic unit of data, a set of key-value pairs (like a JSON object).
- Collection: A group of documents, similar to a table but without a rigid schema.
- Database: A container for collections.
- BSON: Binary JSON, the internal storage format for documents, extending JSON with more data types.
Common Use Cases
MongoDB excels where flexibility and scale are paramount:
- Content Management Systems (CMS): For varied content types.
- Real-time Analytics & IoT: Ingesting and processing large volumes of streaming data.
- Product Catalogs & E-commerce: Managing diverse product attributes.
- Mobile Applications: Scalable backends for app data.
- User Data Management: Storing profiles and preferences.
Basic Operations
MongoDB uses a rich query language (similar to JSON) for standard CRUD (Create, Read, Update, Delete) operations:
- Insert:
db.collection.insertOne()
/insertMany()
- Find:
db.collection.find()
/findOne()
- Update:
db.collection.updateOne()
/updateMany()
- Delete:
db.collection.deleteOne()
/deleteMany()
MongoDB vs. Relational Databases
MongoDB differs from relational databases by:
- Using flexible, schema-less data storage instead of rigid table schemas
- Enabling horizontal scaling by default
- Storing related data together as embedded documents for performance
Key Takeaways
- MongoDB is a powerful NoSQL database known for its flexible schema and scalability.
- It stores data in JSON-like documents, simplifying development.
- Key features include sharding for horizontal scaling and replica sets for high availability.
- Ideal for modern, agile applications with rapidly changing data needs and large data volumes.