0Pricing
MongoDB Academy · レッスン

Outlier パターンと Tree Structure パターン

Outlier パターンを使って極端に大きな配列を持つドキュメントを扱い、親参照またはマテリアライズドパスによって階層型のツリーデータをモデル化します。

「Outlier パターンと Tree Structure パターン」はCoddyKit上の無料MongoDB Academyレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはMongoDB Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 MongoDB Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

The Outlier Problem

Most MongoDB collections have documents that follow a predictable size distribution. But occasionally you get outliers — documents that deviate dramatically from the norm. A social media post that goes viral might accumulate 50,000 comments while typical posts have 5–20. A product liked by a celebrity might have 10,000 reviews. Designing your schema around the average case while ignoring outliers leads to documents that eventually hit the 16 MB document size limit or cause memory pressure.

Detecting Outlier Documents

Before designing around outliers, identify whether they actually exist in your data. Use an aggregation pipeline to find documents with unusually large arrays. Set a threshold based on your expected normal range — if 99% of posts have fewer than 100 comments, documents with more than 1,000 comments are outliers worth handling specially.

// Find posts with outlier-level comment counts
db.posts.aggregate([
  {
    $project: {
      title: 1,
      commentCount: { $size: { $ifNull: ['$comments', []] } }
    }
  },
  { $match: { commentCount: { $gt: 1000 } } },
  { $sort: { commentCount: -1 } },
  { $limit: 10 }
])

The Outlier Pattern: Overflow Flag

The Outlier Pattern keeps the normal case fast by embedding arrays up to a threshold, and handles exceptional documents by setting an hasOverflow flag and storing the overflow items in a separate collection. Application code checks the flag — if false (the common case), it uses the embedded array. If true (the outlier case), it performs an additional query to the overflow collection.

// Normal post document (99% of posts)
{ _id: ObjectId(), title: 'Regular Post', comments: [/* up to 100 */], hasOverflow: false }

// Outlier post document
{
  _id: ObjectId(),
  title: 'Viral Post',
  comments: [/* first 100 comments */],
  hasOverflow: true  // more comments in overflow collection
}

// Overflow collection document
{ postId: ObjectId('...'), comments: [/* comments 101-5000 */] }

Reading With the Outlier Pattern

Application code must handle the outlier flag explicitly. Most of the time, the flag is false and reads are fast. For outlier documents, perform the additional overflow query. This keeps the common path optimised while correctly handling exceptional cases without bloating normal documents or hitting the 16 MB limit.

async function getPostWithComments(postId) {
  const post = await db.collection('posts').findOne({ _id: postId })

  if (!post.hasOverflow) {
    return post  // fast path — all comments embedded
  }

  // Outlier path — fetch additional comments from overflow
  const overflow = await db.collection('postOverflow').findOne({ postId })
  return {
    ...post,
    comments: [...post.comments, ...(overflow?.comments ?? [])]
  }
}

Introduction to Tree Structure Patterns

Hierarchical data — product categories, organisational charts, file systems, comment threads — appears in almost every application. MongoDB does not have a native tree data type, so the structure must be modelled in the document schema. There are four common tree patterns, each optimised for different query access patterns: Parent References, Child References, Array of Ancestors, and Materialised Paths.

Parent References: Simple Hierarchy

The Parent Reference pattern stores each node with a single parent field pointing to its parent's _id. Root nodes have parent: null. This is the simplest representation and mirrors how SQL nested-set or adjacency-list trees work. It is efficient for finding a node's direct parent or direct children, but requires recursive queries to traverse multiple levels.

// Category tree with Parent References
db.categories.insertMany([
  { _id: 1, name: 'Electronics',   parent: null },
  { _id: 2, name: 'Phones',        parent: 1 },
  { _id: 3, name: 'Laptops',       parent: 1 },
  { _id: 4, name: 'Smartphones',   parent: 2 },
  { _id: 5, name: 'Feature Phones',parent: 2 }
])

// Find direct children of 'Electronics'
db.categories.find({ parent: 1 })

Array of Ancestors: Fast Ancestor Lookup

The Array of Ancestors pattern stores the full path from root to the current node in an ancestors array. This makes it trivial to answer 'is X an ancestor of Y?' with a simple array membership check. It also makes it easy to find all descendants of a node — query for documents whose ancestors array contains that node's _id. The tradeoff is that moving a subtree requires updating all descendant documents.

// Array of Ancestors pattern
db.categories.insertMany([
  { _id: 1, name: 'Electronics',   ancestors: [] },
  { _id: 2, name: 'Phones',        ancestors: [1] },
  { _id: 4, name: 'Smartphones',   ancestors: [1, 2] }  // root→Electronics→Phones
])

// Find all descendants of Electronics (id=1)
db.categories.find({ ancestors: 1 })

// Check if Electronics is an ancestor of Smartphones
db.categories.findOne({ _id: 4, ancestors: 1 })  // not null = yes

Materialised Paths: String-Based Tree

The Materialised Path pattern stores the full path as a string (e.g., '/Electronics/Phones/Smartphones'). It enables prefix queries to find all nodes under a subtree, and regex queries to search within path segments. This pattern maps naturally to file system paths or URL hierarchies. It is efficient for both ancestor lookup and descendant enumeration, but can be fragile when nodes are renamed or moved.

// Materialised Path pattern
db.categories.insertMany([
  { _id: 1, name: 'Electronics', path: ',1,' },
  { _id: 2, name: 'Phones',      path: ',1,2,' },
  { _id: 4, name: 'Smartphones', path: ',1,2,4,' }
])

// Find all descendants of Phones (id=2) — path contains ',2,'
db.categories.find({ path: /,2,/ })

// Find the full path ancestors of Smartphones
db.categories.find({ _id: { $in: [1, 2] } })  // parse path and lookup ids

Choosing the Right Tree Pattern

Choose the tree pattern based on your most frequent query: Parent References — simple, good for traversal with application-side recursion; Child References — embed direct children array, fast for reading one level; Array of Ancestors — fast for ancestor lookup and subtree queries, expensive for moves; Materialised Paths — fast regex-based subtree queries, fragile on renames. Hybrid approaches (storing both parent and ancestors) trade write complexity for read speed.

Child References: Embedding Direct Children

The Child References pattern embeds an array of direct child _id values in each node document. This makes it fast to retrieve all children of a node in a single read (no separate query needed). It is ideal for trees that are read top-down frequently (e.g., a menu renders children immediately). The tradeoff is that the children array can grow large for wide nodes, and you cannot efficiently find a node's parent without an additional index or field.

// Child References pattern
db.categories.insertMany([
  { _id: 1, name: 'Electronics', children: [2, 3] },
  { _id: 2, name: 'Phones',      children: [4, 5] },
  { _id: 3, name: 'Laptops',     children: [] },
  { _id: 4, name: 'Smartphones', children: [] },
  { _id: 5, name: 'Feature Phones', children: [] }
])

// Get direct children of Electronics in one read
const parent = db.categories.findOne({ _id: 1 })
const children = db.categories.find({ _id: { $in: parent.children } }).toArray()

Using $graphLookup for Tree Traversal

MongoDB's $graphLookup aggregation stage recursively follows reference fields to traverse a tree or graph stored in any pattern. It returns all reachable nodes up to a specified depth. Use it with Parent References or Child References to traverse hierarchies without writing recursive application code. Specify maxDepth to prevent infinite loops in circular graphs.

// Traverse all descendants of Electronics using $graphLookup
db.categories.aggregate([
  { $match: { _id: 1 } },  // start from Electronics
  {
    $graphLookup: {
      from: 'categories',
      startWith: '$_id',
      connectFromField: '_id',
      connectToField: 'parent',
      as: 'descendants',
      maxDepth: 10
    }
  }
])

Quick Check

Test your understanding of MongoDB & NoSQL Databases concepts from this lesson.

Lesson Recap

In this lesson you learned: the Outlier Pattern keeps normal documents lean by embedding arrays up to a threshold and using a hasOverflow flag to route exceptional documents to an overflow collection, tree structure patterns (Parent References, Array of Ancestors, Materialised Paths) each optimise for different query access patterns on hierarchical data, and $graphLookup recursively traverses references in the pipeline without application-side recursion. Next up we compare MongoDB against Redis for document vs key-value workloads.

よくある質問

「Outlier パターンと Tree Structure パターン」レッスンは無料ですか?

はい。「Outlier パターンと Tree Structure パターン」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、MongoDB Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 MongoDB Academyコースには全4レッスンが含まれています。

「Outlier パターンと Tree Structure パターン」で何を学びますか?

Outlier パターンを使って極端に大きな配列を持つドキュメントを扱い、親参照またはマテリアライズドパスによって階層型のツリーデータをモデル化します。 ブラウザで直接実行するハンズオンコードでMongoDB Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

MongoDB Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのMongoDB Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。

「Outlier パターンと Tree Structure パターン」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このMongoDB Academyレッスンでコードを書いて実行できますか?

はい。すべてのMongoDB Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. Bucket パターンと Computed パターン
  2. Extended Reference パターンと Subset パターン
  3. Polymorphic パターンと Schema Versioning パターン
  4. Outlier パターンと Tree Structure パターン
← MongoDB Academyに戻る