List and get databases and graphs for Azure Cosmos DB - API for MongoDB
APPLIES TO: MongoDB
Note
We recommend that you use the Azure Az PowerShell module to interact with Azure. See Install Azure PowerShell to get started. To learn how to migrate to the Az PowerShell module, see Migrate Azure PowerShell from AzureRM to Az.
This sample requires Azure PowerShell Az 5.4.0 or later. Run Get-Module -ListAvailable Az
to see which versions are installed.
If you need to install, see Install Azure PowerShell module.
Run Connect-AzAccount -Environment AzureChinaCloud to sign in to Azure.
Sample script
# Reference: Az.CosmosDB | https://learn.microsoft.com/powershell/module/az.cosmosdb
# --------------------------------------------------
# Purpose
# Show list and get operations for accounts, databases, and collections
# --------------------------------------------------
# Variables - ***** SUBSTITUTE YOUR VALUES *****
$resourceGroupName = "myResourceGroup" # Resource Group must already exist
$accountName = "myaccount" # Must be all lower case
$databaseName = "myDatabase"
$collectionName = "myCollection"
# --------------------------------------------------
Write-Host "List all accounts in a resource group"
Get-AzCosmosDBAccount -ResourceGroupName $resourceGroupName
Write-Host "Get an account in a resource group"
Get-AzCosmosDBAccount -ResourceGroupName $resourceGroupName `
-Name $accountName
Write-Host "List all databases in an account"
Get-AzCosmosDBMongoDBDatabase -ResourceGroupName $resourceGroupName `
-AccountName $accountName
Write-Host "Get a database in an account"
Get-AzCosmosDBMongoDBDatabase -ResourceGroupName $resourceGroupName `
-AccountName $accountName -Name $databaseName
Write-Host "List all collections in a database"
Get-AzCosmosDBMongoDBCollection -ResourceGroupName $resourceGroupName `
-AccountName $accountName -DatabaseName $databaseName
Write-Host "Get a collection in a database"
Get-AzCosmosDBMongoDBCollection -ResourceGroupName $resourceGroupName `
-AccountName $accountName -DatabaseName $databaseName `
-Name $collectionName
Clean up deployment
After the script sample has been run, the following command can be used to remove the resource group and all resources associated with it.
Remove-AzResourceGroup -ResourceGroupName "myResourceGroup"
Script explanation
This script uses the following commands. Each command in the table links to command specific documentation.
Command | Notes |
---|---|
Azure Cosmos DB | |
Get-AzCosmosDBAccount | Lists Azure Cosmos DB Accounts, or gets a specified Azure Cosmos DB Account. |
Get-AzCosmosDBMongoDBDatabase | Lists API for MongoDB Databases in an Account, or gets a specified API for MongoDB Database in an Account. |
Get-AzCosmosDBMongoDBCollection | Lists API for MongoDB Collections, or gets a specified API for MongoDB Collection in a Database. |
Azure Resource Groups | |
Remove-AzResourceGroup | Deletes a resource group including all nested resources. |
Next steps
For more information on the Azure PowerShell, see Azure PowerShell documentation.