Create an Azure Cosmos DB for NoSQL account
In this exercise, you create an Azure Cosmos DB for NoSQL account, add a database, container, and items in Data Explorer, and query the data. You also use Azure Cloud Shell to grant data access.
This exercise should take approximately 30 minutes to complete.
📝 You need an Azure subscription with permission to create resources and assign Cosmos DB data roles. If you don’t have a subscription, create a free account before you begin. Provisioned throughput can incur charges, so complete the cleanup steps when you finish.
Register the Azure Cosmos DB resource provider
Before you can create a resource type, the resource provider that owns it must be registered to your subscription. Azure Cosmos DB belongs to the Microsoft.DocumentDB provider, and registration is a one-time step per subscription.
-
In a web browser, open the Azure portal and sign in.
-
In the search bar at the top of the portal, enter Subscriptions, and then select Subscriptions in the results.
-
Select the subscription you want to use for this exercise.
-
In the resource menu, under Settings, select Resource providers.
-
In the filter box, enter DocumentDB.
-
Select the Microsoft.DocumentDB row, and then select Register in the toolbar.
-
Select Refresh until the Status column shows Registered. Registration takes a minute or two.
📝 If the status already shows Registered, you don’t need to register this provider again. Registering this provider requires the
Microsoft.DocumentDB/register/actionpermission at the subscription scope. The Owner and Contributor roles include this permission.
If Microsoft.CloudShell isn’t registered, register it in the same subscription before opening Cloud Shell.
Create an Azure Cosmos DB for NoSQL account
Choose Azure Cosmos DB for NoSQL. The account’s API can’t be changed after creation.
-
In the Azure portal, select Create a resource, search for Azure Cosmos DB, and select Create in the results.
-
On Recommended APIs, select Create under Azure Cosmos DB for NoSQL.
-
On the Basics tab, enter the following settings, and leave the remaining settings at their default values:
Setting Value Workload Type Learning Subscription Your Azure subscription Resource Group ResourceGroup1, or your lab-provided group if its name differsAccount Name Enter a globally unique name Location Choose any available region Capacity mode Provisioned throughput If you use your own subscription, create
ResourceGroup1only if it doesn’t already exist. -
Go to the Security section of the account-creation process, and for Key-based authentication, select Disable. The account then accepts only Microsoft Entra ID authentication, which is the recommended approach for new accounts.
-
Select Review + create, and after validation passes, select Create.
-
Wait for the deployment to finish. This step can take a few minutes.
-
When deployment completes, select Go to resource to open your new account.
Grant yourself access to the data
Azure management roles don’t grant access to items. Assign yourself Cosmos DB Built-in Data Contributor to read and write data in Data Explorer.
📝 Use Azure CLI to create the role assignment. Your Azure role must allow reading SQL role definitions and managing SQL role assignments. The Owner and Contributor roles include these permissions.
-
In the Azure portal toolbar, select the Cloud Shell icon (
>_) to open Azure Cloud Shell, and choose the Bash experience if you’re prompted.📝 If you haven’t set up Cloud Shell, in Getting started, select No storage account required, select the subscription you use for this exercise, and then select Apply. Wait for the Bash prompt before running the following commands.
-
Run the following commands, replacing
<account-name>with the account you created. If your lab provides a different resource group name, replaceResourceGroup1with that name. The third command looks up your own identity:RESOURCE_GROUP="ResourceGroup1" ACCOUNT_NAME=<account-name> PRINCIPAL_ID=$(az ad signed-in-user show --query id --output tsv) -
Run the following command to assign yourself the Cosmos DB Built-in Data Contributor role, which permits reading and writing items:
az cosmosdb sql role assignment create \ --resource-group $RESOURCE_GROUP \ --account-name $ACCOUNT_NAME \ --role-definition-id 00000000-0000-0000-0000-000000000002 \ --principal-id $PRINCIPAL_ID \ --scope "/" -
Wait for the command to finish, then close the Cloud Shell pane.
Allow a few minutes for the role assignment to take effect, then refresh Data Explorer.
Create a database and container
Use /categoryId as the partition key so products with the same category ID share a logical partition.
-
In your account’s resource menu, select Data Explorer.
-
In the Data Explorer pane, select New Container.
-
In the New Container panel, enter the following settings, leave the remaining settings at their default values, and then select OK:
Setting Value Database id Create new, cosmicworksContainer id productPartition key /categoryIdContainer throughput Manual RU/s 400 -
In the Data Explorer pane, expand the
cosmicworksdatabase node, and confirm that the product container appears beneath it.
Create items
Add two JSON items to the container. Each item is a document, and items in the same container can differ in shape.
-
In the Data Explorer pane, expand the
cosmicworksdatabase, expand the product container, and then select Items. -
Select New Item on the toolbar. Replace the placeholder JSON with the following content:
{ "categoryId": "4F34E180-384D-42FC-AC10-FEC30227577F", "categoryName": "Components, Pedals", "sku": "PD-R563", "name": "ML Road Pedal", "price": 62.09 } -
Select Save from the toolbar to add the item. Notice that Data Explorer supplies an
id, and the service adds system properties. -
Select New Item again, replace the JSON with the following content, and then select Save:
{ "categoryId": "26C74104-40BC-4541-8EF5-9892F7F03D72", "categoryName": "Components, Saddles", "sku": "SE-R581", "name": "LL Road Seat/Saddle", "price": 27.12 } -
Select each item in the tree to view its full JSON, including the properties the service added.
Query your data
Run a query and inspect its request charge.
-
In the Data Explorer pane, with the product container selected, select New SQL Query.
-
Replace the query text with the following statement, and then select Execute Query:
SELECT * FROM product p WHERE p.price > 50 -
Review the results. The query returns one item, ML Road Pedal, because it’s the only product priced above 50.
-
Select Query Stats and record Request Charge, the number of request units (RUs) used by the query.
-
Change the filter to
p.price > 25and run the query again. Both items match this time, because both prices are above 25.
Clean up your resources
If you created a new resource group for this exercise and it contains only exercise resources, delete the group when you no longer need it.
-
In the Azure portal, open the resource group you created for this exercise.
-
On the toolbar, select Delete resource group.
-
Enter the resource group name to confirm, and then select Delete.
If you used an existing resource group or the group contains resources you need to keep, delete only the Azure Cosmos DB account you created for this exercise. Open the account in the Azure portal, select Delete, and confirm the deletion. Don’t delete the resource group.