This guide will help you explore and manage your DocumentDB database using the powerful DocumentDB for VS Code extension - a dedicated tool that brings your MongoDB-compatible databases right into your editor.
- Why Use the Extension?
- Installation
- Connecting to Your Local Database
- Extension Features
- Working with Data
- Advanced Usage
- Troubleshooting
The DocumentDB for VS Code extension provides:
β¨ Seamless Integration: Browse, query, and manage databases without leaving VS Code
π Multiple View Modes: Table, Tree, and JSON views for different data perspectives
β‘ Smart Query Editor: Auto-complete, syntax highlighting, and field suggestions
π Import/Export: Quick data migration with JSON file support
π― Service Discovery: Auto-detect DocumentDB and MongoDB instances across environments
- Open VS Code
- Click the Extensions icon in the sidebar (or press
Ctrl+Shift+X/Cmd+Shift+X) - Search for "DocumentDB for VS Code"
- Click Install on the extension by Microsoft (
ms-azuretools.vscode-documentdb) - Reload VS Code if prompted
Install from the marketplace link:
code --install-extension ms-azuretools.vscode-documentdbOnce your e-commerce API is running via Docker Compose, you can connect the extension to your local DocumentDB instance.
# Start your services
docker-compose up -d
# Verify DocumentDB is running
docker-compose ps documentdb- Look for the DocumentDB icon in the VS Code Activity Bar (left sidebar)
- Click it to open the DocumentDB view
- You'll see two main sections:
- Connections View: Your saved database connections
- Service Discovery View: Auto-detected databases
-
Click the "+" button or "Add New Connection"
-
Select "Connection String" from the navigation bar
-
Paste your local DocumentDB connection string:
mongodb://admin:password123@localhost:10260/?tls=true&tlsAllowInvalidCertificates=true&authMechanism=SCRAM-SHA-256 -
Important: The connection string format is:
- Protocol:
mongodb:// - Username:
admin(default from docker-compose.yml) - Password:
password123(default from docker-compose.yml) - Host:
localhost:10260(DocumentDB default port) - Required Parameters:
tls=true- Enable TLS/SSLtlsAllowInvalidCertificates=true- Accept self-signed certificates (local dev only)authMechanism=SCRAM-SHA-256- Use SCRAM-SHA-256 authentication
- Protocol:
-
Give your connection a friendly name (e.g., "E-commerce Local Dev")
-
Click Connect
The extension can automatically discover local DocumentDB instances:
- Navigate to the Service Discovery View
- Look for your local connection under detected services
- Click the "Save" icon to add it to your Connections View
- In the Connections View, expand your connection
- You'll see databases created by your FastAPI application:
documentdb(default database name from.env)
- Expand to see collections:
productscustomersorders
Tree View Navigation:
- Connection β Databases β Collections β Documents hierarchy
- Right-click for context menus with actions
- Drag and drop support for organizing connections
Multiple View Modes:
- Table View: Spreadsheet-like view for quick data scanning
- Tree View: Hierarchical document structure exploration
- JSON View: Raw document inspection with syntax highlighting
The extension provides a powerful query interface with IntelliSense:
Features:
- Syntax highlighting for MongoDB queries
- Auto-complete for operators (
$gt,$regex,$in, etc.) - Field name suggestions based on your schema
- Query history for reusing previous queries
Example Queries:
// Find all products in Electronics category
{ category: "Electronics" }
// Find products with price greater than $100
{ price: { $gt: 100 } }
// Find orders with "pending" status
{ status: "pending" }
// Text search in product names
{ name: { $regex: "wireless", $options: "i" } }
// Find products with low stock
{ stock_quantity: { $lt: 10 } }Exporting Data:
- Right-click on a collection
- Select "Export"
- Choose what to export:
- Entire collection
- Current query results
- Individual documents
- Specify output JSON file location
Importing Data:
- Right-click on a collection
- Select "Import"
- Choose your JSON file
- Confirm import
Supported Formats:
- JSON array of documents
- NDJSON (newline-delimited JSON)
- Single document JSON
Create Documents:
- Right-click on a collection β "New Document"
- Edit the template JSON
- Save to insert
Edit Documents:
- Click on a document to open it
- Modify in JSON view
- Press
Ctrl+S(orCmd+S) to save changes
Delete Documents:
- Right-click on a document
- Select "Delete Document"
- Confirm deletion
Create Collections:
- Right-click on a database β "Create Collection"
- Enter collection name
- Optionally configure options (validation, capped collection, etc.)
Drop Collections:
- Right-click on a collection β "Drop Collection"
- Confirm deletion
β οΈ This cannot be undone!
View Indexes:
- Expand collection
- See Indexes node showing all collection indexes
- Review index definitions created by Beanie
After loading sample data or creating records via the API, you can explore them:
Products Collection:
// View all products
{}
// Filter by category
{ category: "Electronics" }
// Find products on sale (assuming you add a sale field)
{ price: { $lt: 100 } }Orders Collection:
// View recent orders (sorted by created_at)
// Use the sort button in the query editor
// Find orders for a specific customer
{ customer_email: "alice.smith@example.com" }
// Find pending orders
{ status: "pending" }Customers Collection:
// Find customers by location
{ "address.city": "San Francisco" }
// Search by name
{ first_name: { $regex: "alice", $options: "i" } }The extension automatically paginates results:
- Default: 20 documents per page
- Navigate with Previous / Next buttons
- Adjust page size in settings:
documentDB.mongoShell.batchSize
For complex queries, use the MongoDB Playground:
- Create a new
.mongodbfile in VS Code - Write aggregation queries:
use('documentdb');
// Total revenue by order status
db.orders.aggregate([
{
$group: {
_id: "$status",
totalRevenue: { $sum: "$total_amount" },
orderCount: { $count: {} }
}
},
{
$sort: { totalRevenue: -1 }
}
]);
// Top-selling products
db.orders.aggregate([
{ $unwind: "$items" },
{
$group: {
_id: "$items.product_name",
totalQuantity: { $sum: "$items.quantity" },
totalRevenue: { $sum: "$items.subtotal" }
}
},
{ $sort: { totalQuantity: -1 } },
{ $limit: 10 }
]);- Click "Execute" to run the pipeline
You can create deep links that open specific connections:
vscode://ms-azuretools.vscode-documentdb?connectionString=<ENCODED_CONNECTION_STRING>&database=documentdb&collection=products
This feature enables:
- Quick bookmarks to frequently-used collections
- Team collaboration via shared links
- Integration with external tools
How to construct URLs: See the extension documentation
The extension integrates with mongosh:
- Install MongoDB Shell: https://www.mongodb.com/docs/mongodb-shell/install/
- Configure path in VS Code settings:
documentDB.mongoShell.path: Path tomongoshexecutable
- Right-click connection β "Launch Shell"
- Run shell commands directly in VS Code terminal
Create .mongodb files for reusable queries:
// File: queries/product-queries.mongodb
// Connect to database
use('documentdb');
// Query 1: Out of stock products
db.products.find({ stock_quantity: { $lte: 0 } });
// Query 2: Premium products
db.products.find({ price: { $gte: 500 } });
// Query 3: Products by category with count
db.products.aggregate([
{ $group: { _id: "$category", count: { $sum: 1 } } }
]);Execute queries by:
- Clicking the "Run" code lens above each query
- Selecting query text and pressing
Ctrl+Shift+R
Multiple Connections:
- Save multiple connections (local, staging, production)
- Color-code with custom names
- Organize with folders (coming soon)
Credential Management:
- Credentials stored securely in VS Code's secret storage
- Update credentials: Right-click β "Update Credentials"
- Different auth methods supported:
- SCRAM-SHA-256 (username/password)
- X.509 certificates
- Connection string-based auth
The extension includes built-in service discovery for:
- Azure Virtual Machines: Auto-discover MongoDB on Azure VMs
- Azure Cosmos DB for MongoDB: Detect vCore and RU-based accounts
- Local Instances: Find DocumentDB Local and emulators
Configure in Service Discovery View β Add Provider
Problem: "Connection timeout" or "Unable to connect"
Solutions:
-
Verify Docker container is running:
docker-compose ps documentdb -
Check connection string parameters:
mongodb://admin:password123@localhost:10260/?tls=true&tlsAllowInvalidCertificates=true&authMechanism=SCRAM-SHA-256 -
Ensure port 10260 is not blocked:
netstat -an | findstr "10260"
-
Check DocumentDB logs:
docker-compose logs documentdb
Problem: "Authentication failed"
Solutions:
-
Verify credentials match
.envfile:- Username:
docdb_user - Password:
docdb_password
- Username:
-
Ensure
authMechanism=SCRAM-SHA-256is in connection string -
Try recreating the connection with fresh credentials
Problem: "SSL handshake failed"
Solution: Ensure these parameters are in your connection string:
tls=true&tlsAllowInvalidCertificates=true
tlsAllowInvalidCertificates=true is acceptable for local development but should never be used in production.
Problem: Collections or documents don't show up
Solutions:
-
Refresh the connection:
- Right-click connection β "Refresh"
-
Verify data exists via API:
# Test API endpoint curl http://localhost:8000/api/v1/products
-
Check database name in connection matches application:
- Default:
documentdb(fromDOCUMENTDB_DB_NAMEin.env)
- Default:
Problem: Slow queries or timeouts
Solutions:
- Check indexes in collections (expand collection β Indexes)
- Limit result set size with filters
- Adjust timeout in settings:
documentDB.mongoShell.timeout: Default 30000ms (30s)
- Monitor Docker container resources:
docker stats documentdb
Access via File β Preferences β Settings (or Ctrl+,):
{
// MongoDB Shell path (if using launch shell feature)
"documentDB.mongoShell.path": "",
// Shell execution timeout (milliseconds)
"documentDB.mongoShell.timeout": 30000,
// Documents per page in query results
"documentDB.mongoShell.batchSize": 20,
// Confirmation prompts
"documentDB.confirmations.confirmationStyle": "Ask",
// Show operation summaries
"documentDB.userInterface.ShowOperationSummaries": true,
// Show URL handling confirmations
"documentDB.confirmations.showUrlHandlingConfirmations": true
}- Extension Homepage: https://marketplace.visualstudio.com/items?itemName=ms-azuretools.vscode-documentdb
- GitHub Repository: https://github.com/microsoft/vscode-documentdb
- Documentation: https://microsoft.github.io/vscode-documentdb/
- Blog Post: https://devblogs.microsoft.com/cosmosdb/meet-the-documentdb-extension-for-vs-code/
- Issue Tracker: https://github.com/microsoft/vscode-documentdb/issues
- Use Descriptive Connection Names: "E-commerce Local" vs "localhost:10260"
- Leverage Query History: Reuse common queries without retyping
- Create
.mongodbFiles: Save frequently-used queries as scrapbooks - Export Before Testing: Backup data before running destructive operations
- Monitor Indexes: Ensure Beanie-created indexes are present for performance
- Share Connection Patterns: Document connection string format in README
- Use URL Deep Links: Share direct links to specific collections
- Export Sample Data: Provide team members with seed data as JSON
- Document Custom Queries: Maintain a shared queries folder in the repo
- β
Use
tlsAllowInvalidCertificates=trueonly for local development - β Never commit connection strings with production credentials
- β Use environment variables for sensitive data
- β Regularly rotate database passwords
- β Restrict DocumentDB access by IP when deploying to cloud
Next Steps: Now that you understand the extension, try:
- Connecting to your local DocumentDB instance
- Exploring the sample data you created
- Running queries to filter products by category
- Exporting a collection to backup your data
- Creating a MongoDB Playground file for analytics queries
Happy querying! π