Documents and Collections
In MongoDB, documents and collections are fundamental concepts that define how data is organized and stored.
Document
- Definition: A document is a basic unit of data in MongoDB, similar to a row in a relational database table. However, documents in MongoDB use a flexible, schema-less structure.
- Format: Documents are stored in BSON (Binary JSON) format, which allows them to store rich, structured data, including arrays and nested documents.
- Example:
{ "name": "Alice", "age": 30, "address": { "street": "123 Main St", "city": "New York" }, "hobbies": ["reading", "traveling", "coding"] }
Collection
- Definition: A collection is a group of MongoDB documents. It is analogous to a table in a relational database. Collections do not enforce a schema, which means documents within a collection can have different fields and structures.
- Example: Consider a collection named
users
. It can contain documents like the ones below:// Document 1 { "name": "Alice", "age": 30, "address": { "street": "123 Main St", "city": "New York" } } // Document 2 { "name": "Bob", "age": 25, "email": "bob@example.com" } // Document 3 { "name": "Charlie", "age": 35, "phone": "555-1234", "hobbies": ["music", "sports"] }
Key Points
- Flexibility: Unlike relational databases, MongoDB collections do not enforce a fixed schema. This means that documents within the same collection can have different fields and data types.
- Schema-less: The schema-less nature of MongoDB collections provides great flexibility, allowing for easy iterations and changes in data models.
In summary, documents are individual records stored in collections, which are groups of documents. This structure provides flexibility and scalability, making MongoDB a powerful choice for many applications.