列出和获取 Azure Cosmos DB 的数据库和图 - MongoDB APIList and get databases and graphs for Azure Cosmos DB - MongoDB API

备注

本文进行了更新,以便使用新的 Azure PowerShell Az 模块。This article has been updated to use the new Azure PowerShell Az module. 你仍然可以使用 AzureRM 模块,至少在 2020 年 12 月之前,它将继续接收 bug 修补程序。You can still use the AzureRM module, which will continue to receive bug fixes until at least December 2020. 若要详细了解新的 Az 模块和 AzureRM 兼容性,请参阅新 Azure Powershell Az 模块简介To learn more about the new Az module and AzureRM compatibility, see Introducing the new Azure PowerShell Az module. 有关 Az 模块安装说明,请参阅安装 Azure PowerShellFor Az module installation instructions, see Install Azure PowerShell.

本示例需要 Azure PowerShell Az 1.0 或更高版本。This sample requires Azure PowerShell Az 1.0 or later. 运行 Get-Module -ListAvailable Az,查看已安装哪些版本。Run Get-Module -ListAvailable Az to see which versions are installed. 如果需要安装,请参阅安装 Azure PowerShell 模块If you need to install, see Install Azure PowerShell module.

运行 Connect-AzAccount -Environment AzureChinaCloud 以登录到 Azure 中国。Run Connect-AzAccount -Environment AzureChinaCloud to sign in to Azure China.

示例脚本Sample script

# Reference: Az.CosmosDB | https://docs.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.

CommandCommand 说明Notes
Azure Cosmos DBAzure Cosmos DB
Get-AzCosmosDBAccountGet-AzCosmosDBAccount 列出 Cosmos DB 帐户或获取指定的 Cosmos DB 帐户。Lists Cosmos DB Accounts, or gets a specified Cosmos DB Account.
Get-AzCosmosDBMongoDBDatabaseGet-AzCosmosDBMongoDBDatabase 列出帐户中的 MongoDB API 数据库,或在帐户中获取指定的 MogoDB API 数据库。Lists MongoDB API Databases in an Account, or gets a specified MongoDB API Database in an Account.
Get-AzCosmosDBMongoDBCollectionGet-AzCosmosDBMongoDBCollection 列出 MongoDB API 集合,或在数据库中获取指定的 MongoDB API 集合Lists MongoDB API Collections, or gets a specified MongoDB API Collection in a Database.
Azure 资源组Azure Resource Groups
Remove-AzResourceGroupRemove-AzResourceGroup 删除资源组,包括所有嵌套的资源。Deletes a resource group including all nested resources.

后续步骤Next steps

有关 Azure PowerShell 的详细信息,请参阅 Azure PowerShell 文档For more information on the Azure PowerShell, see Azure PowerShell documentation.

可以在 Azure Cosmos DB PowerShell 脚本中找到其他 Azure Cosmos DB PowerShell 脚本示例。Additional Azure Cosmos DB PowerShell script samples can be found in the Azure Cosmos DB PowerShell scripts.