MongoDBExplainer
Data Modeling and Schema Design for MongoDB Search Part 2: Use Cases and Patterns
Summary
The post presents best practices for data modeling and schema design in MongoDB Search, covering use cases and patterns such as the Entity Pattern to guide developers in structuring collections for effective search. It emphasizes that searchable items, like movies, cast members, or genres, should be modeled as first-class documents in their own collections rather than as nested structures or ambiguous attributes. The Entity Pattern addresses ‘find anything anywhere’ and as-you-type suggestions by representing all domain entities as documents in a specialized collection with stable identifiers and a type field for filtering. The Attribute Pattern is explicitly discouraged for search because its key-value structure prevents type-sensitive field mapping, making it better to model attributes as straightforward fields. The post details a multi-indexing strategy on the name field that applies left-edge, phonetic, shingled, and language-agnostic analysis to enable flexible matching and relevancy scoring, with optional type-specific metadata nested in sub-documents.
Context
This post is Part 2 of a series on data modeling and schema design for MongoDB Search, building on foundational concerns from Part 1. It covers common use cases and patterns, including the Entity Pattern, Single-Collection Pattern, Tree Pattern, and the pitfalls of the Attribute Pattern for search. The guidance centers on modeling for the end user search experience and using first-class documents for domain entities.
Approach / What changed
The Entity Pattern, which models all searchable domain entities as first-class documents in a specialized collection with a type field and multi-indexed name field to enable findability and as-you-type suggestions.
Takeaways
For search, model each domain entity as a first-class document in its own collection with a type discriminator to enable filtering, faceting, and boosting by entity type.
Avoid the Attribute Pattern for search; instead, model each searchable attribute as a separate field with consistent data types to leverage type-sensitive indexing and mapping.
The Entity Pattern uses multi-indexing on the name field (e.g., left-edge, phonetic, shingling) to match query text in many ways and weigh each analysis for relevancy.
FAQ
Why is the Attribute Pattern problematic for MongoDB Search?
The Attribute Pattern uses a generic key/value structure where all values are indexed under the same field, mixing different data types and preventing type-sensitive field mapping. The embeddedDocuments field type can only help for limited cases, so the best practice is to model each attribute as a separate field.
How does the Entity Pattern support as-you-type suggestions?
The name field is indexed with multiple analysis methods such as left-edge (starts with), phonetic, lowercase, and shingling. In the query, each analysis is weighted differently and combined with boosting for best-match suggestions, enabling flexible matching on partial input.
What is the single-collection pattern and when is it used?
It blends all domain entities into one collection typed by a field. It is practical when there are few entity types and all are already first-class documents in a single collection, essentially the Entity Pattern applied to a single collection.
Source and freshness
- Source
- mongodb.com
- Published
- Added to Yomu
Summaries are generated from the original by a language model. Check the original before relying on it.
Keep reading
Related reading
The Good Stuff
Paid picks we use ourselves. Links carry a referral code — a subscription through one earns us a commission at no extra cost to you.
The best engineering reads, once a week
One useful email. Unsubscribe anytime.