Problem Statement
What is the main advantage of MongoDB's schema-less design?
Explanation
The main advantage of MongoDB's schema-less design is that documents in the same collection can have different structures without requiring schema changes. This flexibility is crucial for agile development where requirements evolve rapidly.
You can add new fields to some documents without affecting others, making it easy to handle varying data types and structures. However, schema-less does not mean schema-free; you should still design your data model thoughtfully. MongoDB also supports schema validation if you need to enforce structure.
Code Solution
SolutionRead Only
// Same collection, different structures - both valid
db.products.insertOne({
name: "Laptop",
price: 999,
specs: { ram: "16GB", cpu: "Intel i7" }
})
db.products.insertOne({
name: "Book",
price: 29,
author: "John Doe",
pages: 350
})