การแมปแหล่งข้อมูล S3 และ Atlas ไปยังเนมสเปซเสมือน
ผู้เรียนจะกำหนดค่าอินสแตนซ์ฐานข้อมูลแบบสหพันธ์ที่แมปคำนำหน้า S3 และคอลเลกชัน Atlas ไปยังฐานข้อมูลและคอลเลกชันเสมือน
การแมปแหล่งข้อมูล S3 และ Atlas ไปยังเนมสเปซเสมือน เป็นบทเรียน MongoDB Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน MongoDB Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส MongoDB Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Storage Configuration: The Core Concept
In Atlas Data Federation, the storage configuration is a JSON document that defines two things: stores (where the raw data lives — S3 buckets, Atlas clusters) and databases/collections (the virtual namespace that applications query). The mapping between them tells the query engine which store to read when you query a virtual collection.
Defining a Store: S3 Bucket
An S3 store definition names the store, specifies the AWS region and bucket name, and associates it with IAM credentials (via an Atlas cloud provider access role). You can optionally set a delimiter and prefix to scope the store to a particular S3 prefix. A single federated instance can have multiple stores pointing to different buckets or regions.
// S3 store definition in storage config
{
'stores': [{
'name': 's3ArchiveStore',
'provider': 'S3',
'region': 'us-east-1',
'bucket': 'mycompany-analytics-archive',
'delimiter': '/',
'additionalStorageClasses': ['STANDARD_IA', 'GLACIER']
}]
}Defining a Store: Atlas Cluster
An Atlas cluster store connects a federated instance to a live Atlas replica set. You reference it by the cluster name within the same Atlas project. This lets you write pipelines that read from live operational collections in the cluster alongside archived S3 data, enabling hybrid queries without any data duplication.
// Atlas cluster store definition
{
'stores': [{
'name': 'liveClusterStore',
'provider': 'atlas',
'clusterName': 'MyProdCluster',
'projectId': 'proj123abc'
}]
}Mapping Collections to S3 Paths
A virtual collection is mapped to a store and a path pattern. The path is a glob pattern that tells Data Federation which S3 objects belong to this collection. The pattern can include literal path segments or wildcards. The query engine will scan all matching objects when the collection is queried.
// Map virtual collection to S3 path pattern
{
'databases': [{
'name': 'analytics',
'collections': [{
'name': 'events_2024',
'dataSources': [{
'storeName': 's3ArchiveStore',
'path': '/data/events/2024/*'
}]
}]
}]
}Partition Attributes in Paths
Partition attributes encode metadata directly in the S3 path using curly-brace syntax: {year int}/{month int}/{day int}/. When a query filters on year, month, or day, Data Federation prunes — skips — all S3 objects whose path does not match the filter values. This is analogous to Hive partitioning and dramatically reduces bytes scanned.
// Path with partition attributes (Data Federation parses the directory structure)
{
'dataSources': [{
'storeName': 's3ArchiveStore',
'path': '/events/{year int}/{month int}/{day int}/*.json'
}]
}
// Query that uses partition pruning:
db.events.find({ year: 2025, month: 3 })
// Data Federation only reads /events/2025/3/ prefixMapping Multiple Sources to One Collection
A single virtual collection can be mapped to multiple data sources — for example, an S3 archive plus a live Atlas collection. Queries merge results from all sources transparently. This is useful for a 'full history' collection where recent data is in Atlas and older data is archived in S3, yet applications query both through a single namespace.
{
'collections': [{
'name': 'orders',
'dataSources': [
{
'storeName': 'liveClusterStore',
'database': 'mydb',
'collection': 'orders' // live Atlas data
},
{
'storeName': 's3ArchiveStore',
'path': '/orders/archive/*.parquet' // S3 archive
}
]
}]
}Wildcard Collections: Schema-on-Read
You can define a wildcard collection (*) that maps all files in an S3 prefix to dynamically named virtual collections. When you query a collection name that matches the wildcard pattern, Data Federation derives the path from the collection name. This is useful for partitioned data lakes where you have thousands of prefix-based 'tables' and cannot enumerate them all in the config.
// Wildcard: each year-month subdirectory becomes a virtual collection
{
'collections': [{
'name': '*',
'dataSources': [{
'storeName': 's3ArchiveStore',
'path': '/data/{collectionName string}/'
}]
}]
}
// Now query any 'table' by name:
db['orders-2024-01'].find({})
db['events-2025-03'].aggregate([...])Updating the Storage Configuration
You can update the storage configuration at any time through the Atlas UI, the Atlas Admin API, or mongosh. Changes take effect immediately — you do not need to restart the federated instance. This lets you add new S3 paths, remap collections to different stores, or add new Atlas cluster sources without any downtime.
// Update storage config via Admin API
// PATCH /api/atlas/v1.0/groups/{groupId}/dataFederation/{name}
// Body: updated storage config JSON
// Or via mongosh using the Atlas admin command
db.adminCommand({
setQueryableEncryptionBackend: 1,
dataFederationConfig: { /* new config */ }
})Testing Your Mapping
After defining the storage configuration, test it by connecting to the federated instance with mongosh and listing databases and collections. Use show dbs, show collections, and a simple find() to verify the mapping is correct. Check that the correct files are being read by inspecting the first few documents returned.
// Connect to federated instance and test
// mongosh 'mongodb+srv://federated.mongodb.net/'
show dbs // lists virtual databases
use analytics
show collections // lists virtual collections
db.events_2024.findOne()
// Verify the document shape matches your S3 filesIAM Role vs Access Key Auth for S3
Atlas Data Federation accesses S3 via AWS IAM. The recommended approach is to use an IAM role delegated to Atlas's AWS account (Atlas Cloud Provider Access) rather than storing access key/secret pairs. The role is granted read access to the S3 bucket via an IAM policy, and Atlas assumes the role when executing queries. This is more secure than long-lived access keys.
// Minimal S3 IAM policy for Data Federation read access
// {
// 'Version': '2012-10-17',
// 'Statement': [{
// 'Effect': 'Allow',
// 'Action': ['s3:GetObject', 's3:ListBucket'],
// 'Resource': [
// 'arn:aws:s3:::mycompany-analytics-archive',
// 'arn:aws:s3:::mycompany-analytics-archive/*'
// ]
// }]
// }Namespace Aliasing and Multiple Views
You can create multiple virtual collections that point to the same underlying S3 prefix with different path patterns — effectively creating multiple views of the same data. For example, one collection exposes all data, another exposes only the current year's partition, and a third maps only Parquet files while excluding JSON. This lets you control what each application tier sees.
Quick Check
Test your understanding of MongoDB & NoSQL Databases concepts from this lesson.
Lesson Recap
In this lesson you learned: stores define where raw data lives (S3 buckets or Atlas clusters) and virtual databases/collections define the namespace applications query, partition attributes in S3 paths enable pruning that dramatically reduces bytes scanned, and a single virtual collection can merge data from multiple stores simultaneously. Next up we write cross-source aggregation pipelines.
เรียนรู้ JavaScript ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 30
- บทเรียน
- 120
คำถามที่พบบ่อย
บทเรียน “การแมปแหล่งข้อมูล S3 และ Atlas ไปยังเนมสเปซเสมือน” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การแมปแหล่งข้อมูล S3 และ Atlas ไปยังเนมสเปซเสมือน” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส MongoDB Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส MongoDB Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การแมปแหล่งข้อมูล S3 และ Atlas ไปยังเนมสเปซเสมือน”
ผู้เรียนจะกำหนดค่าอินสแตนซ์ฐานข้อมูลแบบสหพันธ์ที่แมปคำนำหน้า S3 และคอลเลกชัน Atlas ไปยังฐานข้อมูลและคอลเลกชันเสมือน คุณปฏิบัติ MongoDB Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน MongoDB Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน MongoDB Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “การแมปแหล่งข้อมูล S3 และ Atlas ไปยังเนมสเปซเสมือน” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน MongoDB Academy นี้ได้ไหม
ได้ บทเรียน MongoDB Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- Atlas Data Federation คืออะไร
- การแมปแหล่งข้อมูล S3 และ Atlas ไปยังเนมสเปซเสมือน
- การเรียกใช้ไปป์ไลน์การรวมข้อมูลข้ามแหล่งที่มา
- การแบ่งพาร์ทิชันข้อมูล S3 เพื่อประสิทธิภาพการค้นหา