0
0
MongodbConceptBeginner · 3 min read

Outlier Pattern in MongoDB: What It Is and How It Works

The outlier pattern in MongoDB is a design approach where rarely accessed or large data is stored separately from frequently accessed data to improve performance. It helps keep the main collection fast by moving out unusual or heavy data into a different collection.
⚙️

How It Works

The outlier pattern works by separating uncommon or large pieces of data from the main collection where most queries happen. Imagine a library where most books are small and easy to find, but a few are very large or rarely read. Instead of keeping those large books on the main shelves, the library stores them in a special room to keep the main shelves neat and quick to browse.

In MongoDB, this means you keep the common, small, or frequently used data in one collection, and move the rare or bulky data to another collection. When you need the outlier data, you query the separate collection. This reduces the load on the main collection and speeds up normal queries.

💻

Example

This example shows a user profile collection where most users have small profiles, but some have large documents with many photos stored separately as outliers.
javascript
db.users.insertMany([
  { _id: 1, name: "Alice", age: 30 },
  { _id: 2, name: "Bob", age: 25 },
  { _id: 3, name: "Carol", age: 40 }
])

db.userPhotos.insertOne({ userId: 3, photos: ["photo1.jpg", "photo2.jpg", "photo3.jpg"] })

// Query main user data
const user = db.users.findOne({ _id: 3 })

// Query outlier photos separately
const photos = db.userPhotos.findOne({ userId: 3 })
Output
{ _id: 3, name: "Carol", age: 40 } { userId: 3, photos: ["photo1.jpg", "photo2.jpg", "photo3.jpg"] }
🎯

When to Use

Use the outlier pattern when some data in your collection is very large or rarely accessed compared to the rest. This helps keep your main queries fast and your database efficient.

For example, in a social media app, most user profiles are small, but some users upload many photos or videos. Storing these large media files separately as outliers prevents slowing down profile lookups.

It is also useful when you want to reduce document size limits or avoid loading unnecessary data in common queries.

Key Points

  • The outlier pattern separates rare or large data from common data.
  • It improves query speed on the main collection.
  • Outlier data is stored in a different collection and queried only when needed.
  • Useful for large documents, media files, or rarely accessed info.

Key Takeaways

The outlier pattern stores rare or large data separately to keep main queries fast.
It improves MongoDB performance by reducing document size and query load.
Use it when some data is much bigger or less frequently accessed than the rest.
Outlier data is kept in a separate collection and queried only when needed.