Collections group documents. Unlike rigid SQL tables, new fields can appear as products evolve—plan for compatibility and migrations.
Implicit collection creation
db.events.insertOne({ type: 'click', at: new Date() })First insert creates events if missing.
Practice: Run on practice in mongosh.
Schema versioning pattern
db.users.insertOne({ schemaVersion: 2, email: 'a@b.com', prefs: { theme: 'dark' } })Track schemaVersion in documents so apps can migrate readers gradually.
Capped collections (preview)
Capped collections are fixed-size FIFO logs—rare in app code but useful for telemetry buffers.
Important interview questions and answers
- Q: Is migration required?
A: Yes—application code must handle old and new shapes during rollouts. - Q: Implicit collection risk?
A: Typos in collection names create empty collections silently.
Self-check
- Why add schemaVersion?
- How are collections created?
Tip: Add schemaVersion during migrations so readers handle old shapes.
Interview prep
- Schema-less myth?
- Flexible schema still needs design and migrations.
- schemaVersion?
- Helps apps read old document shapes during rollout.