Tutorial: MongoDB to Cosmos DB Migration with VS Code Extension¶
Duration: 2--4 hours Prerequisites: VS Code installed, Azure subscription, access to source MongoDB instance Outcome: Assessed MongoDB compatibility, planned migration, executed data transfer, and validated results -- all from within VS Code.
Overview¶
The Azure Cosmos DB migration extension for VS Code provides a guided, integrated experience for migrating MongoDB databases to Cosmos DB. This tutorial walks through the complete workflow: installing the extension, connecting to your source MongoDB, running a compatibility assessment, planning the migration, executing the data transfer, and validating the results.
Step 1: Install required VS Code extensions¶
1.1 Install Azure Cosmos DB extension¶
- Open VS Code.
- Open the Extensions panel (
Ctrl+Shift+X/Cmd+Shift+X). - Search for "Azure Databases".
- Install the Azure Databases extension by Microsoft.
- This extension includes Cosmos DB management, data exploration, and the migration assessment tool.
1.2 Install MongoDB extension (optional but recommended)¶
- In the Extensions panel, search for "MongoDB for VS Code".
- Install the MongoDB for VS Code extension by MongoDB.
- This provides a MongoDB playground for running queries against both source and target.
1.3 Sign in to Azure¶
- Open the Command Palette (
Ctrl+Shift+P/Cmd+Shift+P). - Type "Azure: Sign In" and select it.
- Complete the browser-based authentication flow.
- Verify your subscription appears in the Azure panel (left sidebar, Azure icon).
Step 2: Connect to source MongoDB¶
2.1 Add MongoDB connection¶
- In the left sidebar, click the Azure icon.
- Expand Workspace section.
- Click Attached Database Accounts > Attach Database Account.
- Select MongoDB as the API.
-
Enter your MongoDB connection string:
-
Click Connect.
2.2 Explore source data¶
- Expand the newly connected MongoDB account in the tree view.
- Browse databases and collections.
- Right-click a collection and select View Documents to inspect sample data.
- Note the following for each collection:
- Approximate document count
- Average document size
- Index definitions
- Fields used in queries
2.3 Document your inventory¶
Create a migration inventory file in your workspace:
<!-- migration-inventory.md -->
## Source MongoDB Inventory
| Database | Collection | Documents | Avg Size | Indexes | Partition Key Candidate |
| -------- | ---------- | --------- | -------- | ------- | ----------------------- |
| mydb | users | 50,000 | 2 KB | 3 | userId |
| mydb | orders | 500,000 | 4 KB | 5 | customerId |
| mydb | products | 10,000 | 8 KB | 4 | categoryId |
| mydb | sessions | 100,000 | 1 KB | 2 (TTL) | userId |
| mydb | audit_log | 2,000,000 | 0.5 KB | 2 | entityId |
Step 3: Run compatibility assessment¶
3.1 Open assessment tool¶
- Open the Command Palette (
Ctrl+Shift+P). - Type "Cosmos DB: Assess MongoDB Migration" and select it.
- Select the connected MongoDB account.
- Select the database(s) to assess.
- Choose the target: Cosmos DB for MongoDB vCore or Cosmos DB for MongoDB (RU-based).
3.2 Review assessment results¶
The assessment report covers:
Compatibility issues:
- Unsupported aggregation stages or operators (RU-based).
- Documents exceeding size limits (2 MB for RU-based, 16 MB for vCore).
- Unsupported index types.
- Capped collections (RU-based limitation).
- Time-series collections (neither supports natively).
Recommendations:
- Partition key suggestions based on query patterns.
- Indexing policy recommendations.
- Throughput estimates (RU/s for RU-based; tier for vCore).
- Feature compatibility warnings.
Example assessment output:
Assessment Report: mydb → Cosmos DB for MongoDB vCore
═══════════════════════════════════════════════════════
Overall Compatibility: 98% (High)
Collections:
✅ users - Fully compatible
✅ orders - Fully compatible
✅ products - Fully compatible
⚠️ sessions - Capped collection detected. Will migrate as regular collection.
✅ audit_log - Fully compatible
Aggregation Pipelines:
✅ 12/12 pipelines compatible
Indexes:
✅ 16/16 indexes compatible
Warnings:
⚠️ sessions: Capped collection will be converted to regular collection with TTL.
ℹ️ Recommended: Add TTL index on 'lastAccess' field (expireAfterSeconds: 3600).
Estimated Migration Time: 45 minutes (500 GB data)
3.3 Export assessment¶
- Click Export Report in the assessment results panel.
- Save as JSON or markdown for team review.
- Share with your migration team and compliance officers (for federal migrations).
Step 4: Provision Cosmos DB target¶
4.1 Create Cosmos DB account from VS Code¶
- In the Azure panel, right-click Cosmos DB under your subscription.
- Select Create Database Account.
-
Configure:
Setting Value Account name my-cosmos-mongoAPI MongoDB (vCore) or MongoDB (RU-based) Location East US (or your preferred region) Capacity mode Provisioned (or Serverless for dev) -
Click Create and wait for deployment (3--5 minutes).
4.2 Create databases and containers¶
For vCore:
- Right-click the new Cosmos DB account > Create Database.
- Enter database name (match source database name for simplicity).
- Databases and collections will be created during data migration.
For RU-based:
- Right-click the new Cosmos DB account > Create Database.
- Enter database name and throughput (shared or dedicated).
- For each collection, right-click the database > Create Collection.
- Critical: Enter the partition key (shard key) for each collection. This is immutable.
- Set throughput (manual or autoscale) per collection.
4.3 Configure networking¶
- In Azure Portal, navigate to the Cosmos DB account.
- Go to Networking > Firewall and virtual networks.
- Add your current IP address for development access.
- For production, configure Private Endpoint.
Step 5: Execute migration¶
5.1 Configure migration task¶
- Open the Command Palette (
Ctrl+Shift+P). - Type "Cosmos DB: Migrate MongoDB Data" and select it.
- Select the source MongoDB connection.
- Select the target Cosmos DB account.
-
Map source databases/collections to target containers:
Source Target Partition Key mydb.users mydb.users /userId mydb.orders mydb.orders /customerId mydb.products mydb.products /categoryId mydb.sessions mydb.sessions /userId mydb.audit_log mydb.audit_log /entityId -
Configure migration options:
- Migration mode: Offline (one-time copy) or Online (with CDC).
- Parallelism: Number of collections to migrate simultaneously (default: 4).
- Batch size: Documents per batch (default: 1000).
5.2 Start migration¶
- Click Start Migration.
- The extension begins transferring data collection by collection.
-
Progress is displayed in the VS Code output panel:
[Migration] Starting: mydb.users (50,000 documents) [Migration] Progress: mydb.users - 25,000/50,000 (50%) - 2 min elapsed [Migration] Complete: mydb.users - 50,000/50,000 (100%) - 4 min [Migration] Starting: mydb.orders (500,000 documents) [Migration] Progress: mydb.orders - 100,000/500,000 (20%) - 3 min elapsed ...
5.3 Monitor and troubleshoot¶
Common issues during migration:
| Issue | Symptom | Resolution |
|---|---|---|
| Rate limiting (RU-based) | 16500 errors in output | Increase provisioned RU/s temporarily |
| Document too large | Error on specific documents | Split or compress documents > 2 MB |
| Unique key violation | 11000 errors | Deduplicate source data or adjust unique key policy |
| Network timeout | Connection reset errors | Check firewall rules; increase timeout settings |
| Partition key missing | Error on insert | Ensure partition key field exists in every document |
Step 6: Validate migration¶
6.1 Document count verification¶
- In VS Code, connect to the target Cosmos DB account.
- Right-click each collection > View Documents.
- Compare document counts with source.
Use the MongoDB playground in VS Code to run validation queries:
// In MongoDB Playground (connected to target)
use("mydb");
// Count documents
print("users:", db.users.countDocuments());
print("orders:", db.orders.countDocuments());
print("products:", db.products.countDocuments());
print("sessions:", db.sessions.countDocuments());
print("audit_log:", db.audit_log.countDocuments());
6.2 Data integrity validation¶
// Sample-based validation
use("mydb");
// Get a few specific documents and verify content
const sampleOrder = db.orders.findOne({ _id: ObjectId("known-order-id") });
print("Sample order:", JSON.stringify(sampleOrder, null, 2));
// Verify aggregation results match
const totalRevenue = db.orders.aggregate([
{ $match: { status: "completed" } },
{ $group: { _id: null, total: { $sum: "$total" } } },
]);
print("Total revenue:", JSON.stringify(totalRevenue.toArray()));
6.3 Index verification¶
// Verify indexes were created
use("mydb");
print("=== orders indexes ===");
db.orders.getIndexes().forEach((idx) => printjson(idx));
print("=== users indexes ===");
db.users.getIndexes().forEach((idx) => printjson(idx));
6.4 Query performance validation¶
// Run explain on key queries
use("mydb");
const plan = db.orders
.find({ customerId: "cust-123" })
.explain("executionStats");
print("Query plan:", JSON.stringify(plan.executionStats, null, 2));
Step 7: Post-migration steps¶
7.1 Update application connection strings¶
- Update environment variables or Key Vault secrets.
- Deploy application changes to staging.
- Run integration tests against Cosmos DB target.
7.2 Configure monitoring¶
- In Azure Portal, navigate to the Cosmos DB account.
- Go to Diagnostic Settings.
- Enable diagnostics to Log Analytics workspace.
- Create alert rules for:
- RU consumption > 80% of provisioned (RU-based)
- Latency p99 > 50 ms
- 429 error rate > 1%
- Storage approaching limit
7.3 Enable platform integration¶
- Purview: Register Cosmos DB as a data source. Run initial scan.
- Analytical store (RU-based): Verify auto-sync is active via Azure Portal > Data Explorer > Analytical Store tab.
- Change feed: Deploy Azure Functions change feed processor for event-driven integration with the csa-inabox platform.
7.4 Decommission source¶
- Set source MongoDB to read-only (if possible).
- Monitor for 1--2 weeks to confirm no unexpected traffic.
- Take final backup of source for archive.
- Decommission source MongoDB cluster.
Troubleshooting¶
Extension not showing migration option¶
- Ensure you have the latest version of the Azure Databases extension.
- Verify you are signed in to Azure (
Azure: Sign In). - Restart VS Code after installation.
Connection failures¶
- Verify the MongoDB connection string is correct.
- Check firewall rules on both source and target.
- For Atlas, ensure your IP is in the Atlas IP Access List.
- For Cosmos DB, ensure your IP is in the firewall allow list.
Migration hangs or fails¶
- Check the VS Code output panel for error details.
- For RU-based: temporarily increase throughput to 50,000+ RU/s.
- For large collections: reduce batch size to 500 or 100.
- For network issues: verify VNet peering or private endpoint configuration.
Related resources¶
Maintainers: csa-inabox core team Last updated: 2026-04-30