Learn about MongoDB, a popular NoSQL database that stores data in flexible, JSON-like documents instead of rigid tables.
What is MongoDB?
MongoDB is a popular NoSQL database that stores data in flexible, JSON-like documents instead of the rigid table structure used in traditional relational databases.
Understanding MongoDB
MongoDB was developed by MongoDB Inc. and released in 2009. It's designed to handle large amounts of unstructured or semi-structured data, making it ideal for modern applications that need flexibility and scalability.
Key Features of MongoDB
1. Document-Oriented Storage
Data is stored in BSON (Binary JSON) documents, which are similar to JSON objects but with additional data types.
2. Schema Flexibility
Unlike relational databases, MongoDB doesn't require a predefined schema, allowing you to store different types of data in the same collection.
3. Horizontal Scalability
MongoDB can scale horizontally by distributing data across multiple servers using sharding.
4. High Performance
Optimized for read and write operations with support for indexing and aggregation.
Basic MongoDB Example
// Insert a document
db.users.insertOne({
name: "John Doe",
email: "john@example.com",
age: 30,
hobbies: ["reading", "gaming"],
address: {
street: "123 Main St",
city: "New York",
zip: "10001"
}
});
// Query documents
db.users.find({ age: { $gte: 25 } });
// Update a document
db.users.updateOne(
{ name: "John Doe" },
{ $set: { age: 31 } }
);
MongoDB vs Relational Databases
| Feature | MongoDB | MySQL/PostgreSQL |
|---------|---------|------------------|
| Data Model | Document | Table |
| Schema | Flexible | Rigid |
| Relationships | Embedding/References | Foreign Keys |
| Scalability | Horizontal | Vertical |
| Query Language | MongoDB Query Language | SQL |
| ACID Compliance | Limited | Full |
Why Use MongoDB?
Common Use Cases
MongoDB Ecosystem
MongoDB has become one of the most popular NoSQL databases, particularly in the JavaScript/Node.js ecosystem, where its document-based approach aligns well with JSON data structures.