What is a collection in MongoDB?
Answer
A collection is a group of MongoDB documents, analogous to a table in a relational database. Collections don't enforce a schema by default — documents in the same collection can have different structures (though in practice, they usually store the same type of data). Key points: (1) Collections exist within a database; a MongoDB deployment can have multiple databases, each with multiple collections; (2) Collections are created lazily — when you first insert a document; (3) Capped collections: fixed-size collections that maintain insertion order and automatically overwrite oldest documents when full. Useful for logs, caches; (4) Naming: collection names can contain any UTF-8 characters except null, $, and system prefix; recommended: lowercase, snake_case (e.g., user_profiles); (5) Schema validation: MongoDB supports optional JSON Schema validation rules on collections, enforcing field types and required fields while keeping flexibility. Commands: db.createCollection("orders") (explicit creation); show collections (list all); db.orders.drop() (remove collection + all documents).