여러 원본에 걸친 집계 파이프라인 실행하기
학습자는 하나의 쿼리에서 Atlas 컬렉션 데이터와 S3에 저장된 JSON 또는 Parquet 파일을 조인하는 집계 파이프라인을 작성합니다.
여러 원본에 걸친 집계 파이프라인 실행하기은(는) CoddyKit의 무료 MongoDB Academy 강의입니다. 이것은 4개 중 3번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 MongoDB Academy 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. MongoDB Academy 강의에는 총 4개의 강의가 포함되어 있습니다.
이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.
What Makes Cross-Source Pipelines Special?
A cross-source aggregation pipeline in Atlas Data Federation runs the same aggregation stages you know from MongoDB, but the data under each stage may come from different physical systems — an S3 bucket, a live Atlas cluster, or both. The federated query engine handles all the routing, fan-out, and result merging transparently. From your application's perspective, it looks like a single MongoDB collection query.
Simple Cross-Source Find
The simplest cross-source query is a find() on a virtual collection backed by S3 files. The query engine reads the files, parses them, and applies the filter. Fields in the filter that match partition attributes in the path cause automatic file pruning. Fields that do not match partition attributes are applied as a post-read filter.
// Virtual collection 'events' backed by S3 JSON files
// Path: /data/events/{year int}/{month int}/*.json
// This query prunes to /data/events/2025/1/ only
const jan2025 = await db.collection('events').find({
year: 2025,
month: 1,
eventType: 'purchase' // post-read filter (not a partition attr)
}).toArray()Aggregating S3 Data Like a Live Collection
You can run any aggregation stage against S3-backed virtual collections: $match, $group, $project, $sort, $limit. The query engine pushes down stages where possible (especially $match for partition pruning and column pruning in Parquet) and executes remaining stages in its own compute layer after reading the data.
// Group S3-archived events by region and count
const summary = await db.collection('events_2024').aggregate([
{ $match: { year: 2024, month: { $in: [10, 11, 12] } } }, // pruning
{ $group: { _id: '$region', total: { $sum: 1 }, revenue: { $sum: '$amount' } } },
{ $sort: { revenue: -1 } },
{ $limit: 10 }
]).toArray()Joining Atlas and S3 With $lookup
The most powerful cross-source pattern is using $lookup to join a live Atlas collection with an S3-archived collection. Start the pipeline from the live collection (the 'driver') and look up into the virtual S3-backed collection. Always place a $match early to minimize the number of lookups performed.
// Join live customers (Atlas) with archived orders (S3)
const result = await db.collection('customers').aggregate([
{ $match: { tier: 'gold', region: 'EU' } }, // filter live data first
{ $lookup: {
from: 'orders_archive', // virtual S3-backed collection
let: { custId: '$_id' },
pipeline: [
{ $match: { $expr: { $eq: ['$customerId', '$$custId'] } } },
{ $project: { orderId: 1, amount: 1, date: 1 } }
],
as: 'orderHistory'
}},
{ $addFields: { totalSpend: { $sum: '$orderHistory.amount' } } },
{ $sort: { totalSpend: -1 } },
{ $limit: 50 }
]).toArray()Aggregating Across Multiple Atlas Clusters
If your federated instance has multiple Atlas cluster stores, you can join collections from different Atlas clusters in a single pipeline. This is useful for multi-tenant or multi-region deployments where data is sharded across separate clusters and you need cross-cluster reports without merging clusters or building a separate reporting database.
// Virtual collections pointing to different Atlas clusters
// 'orders_us' -> Atlas cluster in US
// 'orders_eu' -> Atlas cluster in EU
// Union results from two clusters
db.orders_us.aggregate([
{ $match: { date: { $gte: ISODate('2025-01-01') } } },
{ $unionWith: {
coll: 'orders_eu',
pipeline: [{ $match: { date: { $gte: ISODate('2025-01-01') } } }]
}},
{ $group: { _id: '$status', count: { $sum: 1 } } }
])Writing Results to Atlas With $out / $merge
After running a cross-source aggregation, you can write the results back to a live Atlas collection using $out or $merge. This is the ETL pattern: read historical data from S3, join with live data, compute aggregates, and write the results to a materialised view collection in Atlas that application queries can then read cheaply and quickly.
// ETL: aggregate S3 archive + Atlas, write result to Atlas
db.events_2024.aggregate([
{ $match: { year: 2024 } },
{ $group: {
_id: { region: '$region', month: '$month' },
sessions: { $sum: 1 },
revenue: { $sum: '$amount' }
}},
{ $merge: {
into: { db: 'reporting', coll: 'monthly_summary' },
whenMatched: 'replace',
whenNotMatched: 'insert'
}}
])Parquet Column Pruning: Only Read What You Need
When querying Parquet files, Data Federation applies column pruning: if your $project stage specifies only certain fields, the query engine reads only those columns from the Parquet file (which stores data column-by-column). This can reduce bytes read by 90%+ compared to reading every column. Place your $project as early as possible in the pipeline for maximum column pruning benefit.
// Column pruning: only reads 'region', 'amount', 'date' columns from Parquet
db.events_2024.aggregate([
{ $project: { region: 1, amount: 1, date: 1, _id: 0 } }, // early project
{ $match: { region: 'EU' } },
{ $group: { _id: '$region', totalRevenue: { $sum: '$amount' } } }
])
// Other columns (userId, sessionId, metadata, etc.) are never read from diskFederated Query Performance Monitoring
Atlas Data Federation logs query execution details in the Atlas UI under the Query History tab. Each query shows: bytes processed, execution time, and the number of files/partitions scanned. High bytes-processed numbers usually mean partition attributes are missing or the query does not match any partition keys. Use this log to tune your storage configuration and query patterns.
// Get query stats via the admin DB on the federated instance
db.adminCommand({ currentOp: 1 })
// Shows active federated queries with bytes read, duration
// In Atlas UI: Data Federation > Query History
// Shows past queries, duration, data processed, and cost estimateHandling Schema Differences Across Sources
S3 files from different time periods or systems may have different schemas (field names, types, structure). Data Federation handles this gracefully — missing fields return null, extra fields are included. You can use $ifNull, $cond, and $convert in your pipeline to normalise varying schemas before grouping or joining.
// Normalise schema variations across old and new S3 file formats
db.events.aggregate([
{ $addFields: {
// Old format: 'user_id', New format: 'userId'
userId: { $ifNull: ['$userId', '$user_id'] },
// Old format: string amount, New format: number
amount: { $convert: { input: '$amount', to: 'double', onError: 0 } }
}},
{ $group: { _id: '$userId', total: { $sum: '$amount' } } }
])Caching Federated Query Results
Data Federation does not cache results between queries — each query re-reads the underlying sources. For dashboards that run the same report repeatedly, use the ETL pattern: schedule an aggregation that writes results to an Atlas collection via $merge, then have your dashboard query the fast Atlas collection. Atlas Triggers can schedule this refresh on any cron interval.
Limitations of Cross-Source Pipelines
Be aware of current limitations: 1) Transactions are not supported on federated instances. 2) Index usage only applies to Atlas-backed collections, not S3 files. 3) Very large result sets may time out — use $out/$merge to write results instead of streaming them back. 4) Latency is higher than a live Atlas query due to S3 I/O — not suitable for user-facing, real-time queries.
Quick Check
Test your understanding of MongoDB & NoSQL Databases concepts from this lesson.
Lesson Recap
In this lesson you learned: cross-source aggregation pipelines use the same MongoDB stages against virtual collections backed by S3 or Atlas clusters, $lookup enables joining live Atlas data with S3 archives in a single pipeline, and early $project enables column pruning in Parquet files to dramatically reduce bytes scanned. Next up we explore S3 data partitioning for query performance.
AI 튜터와 함께 JavaScript을(를) 배우세요 — 무료
브라우저에서 실제 코드를 작성하고 실행하며, 24/7 AI 튜터로부터 즉각적인 도움을 받고, 웹이나 앱에서 중단한 부분부터 계속 학습하세요.
- 코스
- 30
- 레슨
- 120
자주 묻는 질문
“여러 원본에 걸친 집계 파이프라인 실행하기” 강의는 무료인가요?
네 — “여러 원본에 걸친 집계 파이프라인 실행하기” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 MongoDB Academy 강의 전체를 잠금 해제할 수 있습니다. MongoDB Academy 강의에는 총 4개의 강의가 포함되어 있습니다.
“여러 원본에 걸친 집계 파이프라인 실행하기”에서 뭘 배우나요?
학습자는 하나의 쿼리에서 Atlas 컬렉션 데이터와 S3에 저장된 JSON 또는 Parquet 파일을 조인하는 집계 파이프라인을 작성합니다. 브라우저에서 직접 실행하는 실습 코드로 MongoDB Academy을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
MongoDB Academy을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 MongoDB Academy은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 3번째 강의입니다.
“여러 원본에 걸친 집계 파이프라인 실행하기” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 MongoDB Academy 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 MongoDB Academy 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- Atlas Data Federation이란 무엇인가요?
- S3 및 Atlas 원본을 가상 네임스페이스에 매핑하기
- 여러 원본에 걸친 집계 파이프라인 실행하기
- 쿼리 성능을 위한 S3 데이터 파티셔닝