MongoDB Schema Patterns for Integration Use Cases
Data & BI
Data & Analytics
MongoDB's document model is well-suited for storing heterogeneous integration payloads, audit logs, and flexible business objects that evolve over time. These are the schema patterns I use for integration-heavy applications.
Key Patterns & Steps
- Embed related data when you always read it together; reference when the sub-document is large or accessed independently
- Integration message store: one document per message with fields for correlationId, status, payload, timestamps, and retry count
- Polymorphic documents: use a type discriminator field when storing different message formats in the same collection
- Index strategy: index on correlationId, status, and createdAt minimum — queries without indexes will table-scan at scale
- TTL index: automatically expire processed messages after a retention period — critical for audit log collections
- Change streams: subscribe to collection changes for event-driven processing without polling
Lessons Learned
The MongoDB collection I had to remediate had no indexes and 50M documents. A query that should take milliseconds took 40 seconds. Schema and index design at collection creation is far cheaper than a live remediation.
Technologies
MongoDBNoSQLSchema DesignIntegrationIndexingChange Streams
← Back to Architecture Notes