Create and connect to a cluster
In this exercise, you create an Azure DocumentDB cluster, connect to it using MongoDB Shell, and run test queries. This exercise uses the Azure portal and Cloud Shell.
Create the cluster
-
Sign in to the Azure portal.
-
Select Create a resource.
-
Search for and select Azure DocumentDB (with MongoDB compatibility).
-
Select Azure DocumentDB (with MongoDB compatibility) again.
-
Select Create to open the cluster configuration page.
-
On the Basics tab, select Configure in the Cluster tier section. On the Scale page, configure these settings and then select Save:
Setting Value Cluster tier M25 (2 burstable vCores, 8-GiB RAM) Storage per shard 32 GiB -
Complete the remaining Basics settings:
Setting Value Subscription Your Azure subscription Resource group Create new: rg-documentdb-learn Cluster name docdb-ecommerce-<your-initials>-<12345> (replace <your-initials>and<12345>with your initials and a random five-digit number to make it globally unique)Location A supported region near you MongoDB version 8.0 Admin username docdbadmin Password A strong password (record it securely) -
Select Next: Networking.
-
Configure networking:
Setting Value Connectivity method Public access Allow public access from Azure services Enabled -
Select + Add current client IP address to add a firewall rule for your machine.
-
Select Review + create, review the settings, then select Create.
-
Wait for deployment to complete (this deployment takes a few minutes), then select Go to resource.
Get the connection string
-
On the cluster page, select Connection strings from the resource menu.
-
Two connection strings may be listed: one for global read-write access and one for the current cluster (Self). Copy the Self connection string. It looks similar to:
mongodb+srv://docdbadmin@docdb-ecommerce-<your-initials>-<12345>.mongocluster.cosmos.azure.com/?tls=true&authMechanism=SCRAM-SHA-256&retrywrites=false&maxIdleTimeMS=120000 -
The password isn't included in the connection string. You enter it when connecting.
Enable Cloud Shell access
Before using the Quick Start experience, allow Azure Cloud Shell to connect to your cluster:
- In your cluster resource page, select Networking from the navigation menu.
- In the Public access section, select Allow public access from Azure services and resources within Azure to this cluster.
- Select Save.
Connect with MongoDB Shell
- In your cluster resource page, select Quick start from the navigation menu.
- Select Open MongoDB shell.
- Wait for the Cloud Shell environment to start.
- Accept the notice by entering Y.
- Enter your admin password when prompted.
After authentication, you see the "Non-Genuine MongoDB" warning. This warning is expected and can be safely ignored.
Run test queries
Verify your connection and explore the cluster:
// Check connection status
db.runCommand({connectionStatus: 1})
Confirm you see ok: 1 in the response. Then create a database and insert your first product document for the Cosmicworks catalog:
// Switch to a new database
use cosmicworks
// Insert a sample product document
db.products.insertOne({
sku: "BK-M82S-38",
name: "Mountain-100 Silver, 38",
category: { name: "Mountain Bikes" },
parentCategory: "Bikes",
price: 3399.99,
inventory: 45,
tags: ["mountain", "aluminum", "disc-brake", "suspension", "high-performance"],
isActive: true,
createdAt: new Date()
})
// Verify the document was inserted
db.products.find().limit(5)
The output shows your inserted document with an autogenerated _id field.
Try a few more operations:
// Insert multiple products
db.products.insertMany([
{
sku: "BK-R93R-52",
name: "Road-150 Red, 52",
category: { name: "Road Bikes" },
parentCategory: "Bikes",
price: 3578.27,
inventory: 28,
tags: ["road", "carbon-fiber", "racing", "high-performance", "lightweight"],
isActive: true,
createdAt: new Date()
},
{
sku: "HL-U509",
name: "Sport-100 Helmet, Black",
category: { name: "Helmets" },
parentCategory: "Accessories",
price: 34.99,
inventory: 320,
tags: ["adjustable", "reflective", "lightweight", "breathable"],
isActive: true,
createdAt: new Date()
}
])
// Query products by parent category
db.products.find({ parentCategory: "Accessories" })
// Count total products
db.products.countDocuments()
You now have a working Azure DocumentDB cluster with sample Cosmicworks product data.
Clean up resources
Delete the resource group to avoid charges:
- In the Azure portal, search for and select Resource groups.
- Select rg-documentdb-learn.
- Select Delete resource group.
- Enter the resource group name to confirm, then select Delete.