Anti Patterns
Unbounded Arrays
- an array without a size limit
- used subset pattern or extended reference pattern
Bloated Docments
- A document that groups together related data that is accessed separately
- say a book store has two pages: home and detailed page. Home shows 10 most popular books, each book displays title and author info. Detail page shows more data for the book, description, SKU, reviews, etc.
- if we use one collection to store the book details, and use that collection for the home page, it's bloated because home page only need title and author.
- a better design would be to create another collection to store title and author only. and the original collection can be used for the detailed page.
- yes it creates data duplicates, but again, it's a trade-off between duplicate data and performance.
Massive Number of Collections
recommended limits:
- 5k per M10 cluster
- 10k per M20/M30 cluster
- 10k per replica set
- 10k per shard
solution:
- update schema design
- sharding
Unnecessary Indexes
- hide them or drop them if they're not used or rarely used
Data Normalization
- separates data that access together into different different collections
- use subset pattern or extended referenced pattern
Case-Sensitivity
- MongoDB is case-sensitive by default
use Collation
- language specific rules
- determine how characters are compared and sorted
- specify locale for desired language, e.g. locale: 'en'
- strength 1-5 (default is 3) (1-2: case insensitive, 3-5: case sensitive)
- collation can be set to collections
reges with \i to do case insensitive search is NOT recommended because it's not efficient.
Use collation instead:
db.Books.find(
{title: 'Practical MongoDB Aggregations' }
).collation({locale: 'en', strength: 2})
Sometimes indexes are case sensitive, so search wouldn't use the indexes to search. To fix it, declare the index with collation:
db.Books.dropIndex({ author: 1 });
db.Books.createIndex(
{ author: 1 },
{
collation: {
locale: 'en',
strength: 2
}
}
);