0Pricing
MongoDB Academy · 강의

파이프라인 개념: 단계, 연산자, 표현식

학습자는 데이터가 파이프라인 단계를 거쳐 흐르는 방식을 이해하고 단계 연산자와 표현식 연산자를 구분합니다.

파이프라인 개념: 단계, 연산자, 표현식은(는) CoddyKit의 무료 MongoDB Academy 강의입니다. 이것은 4개 중 1번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 MongoDB Academy 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. MongoDB Academy 강의에는 총 4개의 강의가 포함되어 있습니다.

이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.

What Is the Aggregation Pipeline?

The aggregation pipeline is MongoDB's server-side data transformation engine. Instead of retrieving raw documents and processing them in application code, you define a sequence of stages that transform the data stream step by step—filtering, reshaping, grouping, computing, and sorting—all within the database engine. This keeps heavy computation close to the data and dramatically reduces what you send over the network.

// A simple aggregation pipeline
db.orders.aggregate([
  { $match: { status: 'completed' } },   // stage 1: filter
  { $group: { _id: '$userId', total: { $sum: '$amount' } } },  // stage 2: group
  { $sort: { total: -1 } },             // stage 3: sort
  { $limit: 10 }                        // stage 4: take top 10
]);

How Documents Flow Through Stages

Think of the pipeline as a conveyor belt: each document enters stage 1, is transformed (or filtered out), and the results flow into stage 2, then stage 3, and so on. At each stage, the set of documents can shrink (filtering), expand (unwind), or be completely replaced by computed summaries (group). Documents that fail a filter condition are dropped from the pipeline and never reach later stages.

// Data flow visualization:
// Input: 10,000 orders
// Stage 1 ($match: status='completed'):  8,000 docs
// Stage 2 ($group by userId):            1,200 docs (one per user)
// Stage 3 ($sort by total DESC):         1,200 docs (reordered)
// Stage 4 ($limit 10):                      10 docs
// Output to client: 10 docs

Stage Operators vs Expression Operators

The aggregation framework distinguishes between two kinds of operators: stage operators (prefixed with $) that define what a pipeline stage does—like $match, $group, $project—and expression operators (also prefixed with $) that compute values within a stage—like $sum, $avg, $concat. Stages are the building blocks; expressions are the calculations inside them.

db.sales.aggregate([
  // $group is a STAGE operator
  { $group: {
    _id: '$region',
    // $sum and $avg are EXPRESSION operators (accumulators here)
    totalRevenue: { $sum: '$amount' },
    avgOrder: { $avg: '$amount' },
    // $concat is an expression operator
    label: { $concat: ['Region: ', '$region'] }
  }}
]);

Field References With the $ Sign

Inside aggregation expressions, a string prefixed with $ is a field reference—it refers to the value of that field in the current document. Without the prefix, a string is treated as a literal value. This distinction is crucial: '$price' means 'the value of the price field' while 'price' means the literal string 'price'.

db.products.aggregate([
  { $project: {
    name: 1,
    // '$price' references the 'price' field value
    discountedPrice: { $multiply: ['$price', 0.9] },
    // 'price' without $ is a literal string
    label: 'price',  // all docs get the string 'price', not the field value
    // $$ROOT is a special variable for the whole document
    original: '$$ROOT'
  }}
]);

System Variables: $$ROOT, $$NOW, $$CURRENT

The aggregation pipeline provides system variables prefixed with $$ that give access to special values: $$ROOT refers to the entire current document, $$NOW is the current datetime (useful in $project for computed fields), $$CURRENT is the current document field path context, and $$REMOVE conditionally removes a field when assigned as a value.

db.orders.aggregate([
  { $project: {
    _id: 1,
    orderDate: 1,
    daysOld: {
      $divide: [
        { $subtract: ['$$NOW', '$orderDate'] },  // $$NOW = current time
        1000 * 60 * 60 * 24  // convert ms to days
      ]
    },
    // Conditionally remove a field:
    sensitiveField: { $cond: { if: '$isAdmin', then: '$secret', else: '$$REMOVE' } }
  }}
]);

Common Pipeline Stages Overview

MongoDB provides dozens of pipeline stages. The most essential ones to know are: $match (filter documents), $project (reshape/compute fields), $group (aggregate by key), $sort (order results), $limit and $skip (pagination), $lookup (join another collection), and $unwind (flatten arrays). Master these and you can express almost any analytical query.

// Quick reference of the most-used stages:
// $match   - { $match: { field: condition } }
// $project - { $project: { keep: 1, drop: 0, computed: expr } }
// $group   - { $group: { _id: '$field', agg: { $sum: '$val' } } }
// $sort    - { $sort: { field: 1 or -1 } }
// $limit   - { $limit: N }
// $skip    - { $skip: N }
// $lookup  - { $lookup: { from, localField, foreignField, as } }
// $unwind  - { $unwind: '$arrayField' }

Pipeline Optimization: Stage Order Matters

MongoDB's query optimizer automatically reorders some pipeline stages for efficiency—for example, moving $match before $sort and $group when possible. However, you should always place $match as early as possible yourself: early filtering reduces the number of documents subsequent stages must process, and if $match is the first stage, MongoDB can use an index for it.

// BAD: expensive group before filter
db.orders.aggregate([
  { $group: { _id: '$userId', total: { $sum: '$amount' } } },
  { $match: { total: { $gt: 1000 } } }  // too late to use index
]);

// GOOD: filter first, then group
db.orders.aggregate([
  { $match: { status: 'completed', createdAt: { $gte: thisYear } } },
  { $group: { _id: '$userId', total: { $sum: '$amount' } } },
  { $match: { total: { $gt: 1000 } } }
]);

Arithmetic Expression Operators

Arithmetic expressions let you compute new values from existing fields within a stage. The most common ones are: $add, $subtract, $multiply, $divide, $mod, and $abs. These operators work on field references, literals, or the result of other expressions—enabling complex calculations that previously required application-side processing.

db.invoices.aggregate([
  { $project: {
    subtotal: '$subtotal',
    taxAmount: { $multiply: ['$subtotal', 0.08] },  // 8% tax
    total: { $add: ['$subtotal', { $multiply: ['$subtotal', 0.08] }] },
    discount: { $subtract: ['$listPrice', '$salePrice'] },
    margin: { $divide: [{ $subtract: ['$price', '$cost'] }, '$price'] }
  }}
]);

Conditional Expressions: $cond and $ifNull

Conditional expressions allow if/else logic inside aggregation stages. $cond evaluates a boolean expression and returns one of two values (like a ternary operator). $ifNull returns a fallback value when a field is null or missing. These are essential for handling optional fields and computing derived categories without modifying the stored data.

db.products.aggregate([
  { $project: {
    name: 1,
    price: 1,
    // Ternary: label products as budget or premium
    tier: {
      $cond: {
        if: { $lt: ['$price', 100] },
        then: 'budget',
        else: 'premium'
      }
    },
    // Fallback for missing description
    description: { $ifNull: ['$description', 'No description available'] }
  }}
]);

String Expression Operators

String operators let you manipulate text fields within the pipeline: $concat joins strings, $toUpper/$toLower change case, $trim/$ltrim/$rtrim strip whitespace, $substr extracts a substring, and $split splits on a delimiter. These are useful for normalising data during aggregation without needing to update the stored documents.

db.users.aggregate([
  { $project: {
    // Combine first and last name
    fullName: { $concat: ['$firstName', ' ', '$lastName'] },
    // Normalise email to lowercase
    emailNorm: { $toLower: '$email' },
    // Extract domain from email
    domain: {
      $arrayElemAt: [{ $split: ['$email', '@'] }, 1]
    }
  }}
]);

Date Expression Operators

Date operators extract components from date fields for grouping and computation: $year, $month, $dayOfMonth, $hour, $minute, $dayOfWeek, and $dateToString. These are indispensable for time-series analytics like 'total orders per month' or 'average revenue by day of week'.

// Group orders by year and month
db.orders.aggregate([
  { $group: {
    _id: {
      year: { $year: '$createdAt' },
      month: { $month: '$createdAt' }
    },
    count: { $sum: 1 },
    revenue: { $sum: '$amount' }
  }},
  { $sort: { '_id.year': 1, '_id.month': 1 } }
]);

Quick Check

Test your understanding of aggregation pipeline concepts from this lesson.

Lesson Recap

In this lesson you learned: pipeline stages transform a stream of documents sequentially, stage operators define what a stage does while expression operators compute values within stages, and $ prefixes field references while $$ prefixes system variables. Next up we focus on $match and $project, the filter and reshape workhorses.

자주 묻는 질문

“파이프라인 개념: 단계, 연산자, 표현식” 강의는 무료인가요?

네 — “파이프라인 개념: 단계, 연산자, 표현식” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 MongoDB Academy 강의 전체를 잠금 해제할 수 있습니다. MongoDB Academy 강의에는 총 4개의 강의가 포함되어 있습니다.

“파이프라인 개념: 단계, 연산자, 표현식”에서 뭘 배우나요?

학습자는 데이터가 파이프라인 단계를 거쳐 흐르는 방식을 이해하고 단계 연산자와 표현식 연산자를 구분합니다. 브라우저에서 직접 실행하는 실습 코드로 MongoDB Academy을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.

MongoDB Academy을(를) 시작하는 데 경험이 필요한가요?

사전 경험은 필요하지 않습니다. CoddyKit의 MongoDB Academy은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 1번째 강의입니다.

“파이프라인 개념: 단계, 연산자, 표현식” 강의는 얼마나 걸리나요?

대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.

이 MongoDB Academy 강의에서 코드를 작성하고 실행할 수 있나요?

네. 모든 MongoDB Academy 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.

이 강의의 모든 강의

  1. 파이프라인 개념: 단계, 연산자, 표현식
  2. $match와 $project: 필터링 및 형태 변경
  3. $group: 집계 및 합계 계산
  4. 파이프라인에서 $sort, $limit, $skip 사용하기
← MongoDB Academy(으)로 돌아가기