Dela via


Lista och hämta databaser och grafer för Azure Cosmos DB – API för MongoDB

GÄLLER FÖR: MongoDB

Kommentar

Vi rekommenderar att du använder Azure Az PowerShell-modulen för att interagera med Azure. Information om hur du kommer igång finns i Installera Azure PowerShell. Information om hur du migrerar till Az PowerShell-modulen finns i artikeln om att migrera Azure PowerShell från AzureRM till Az.

Det här exemplet kräver Azure PowerShell Az 5.4.0 eller senare. Kör Get-Module -ListAvailable Az för att se vilka versioner som är installerade. Om du behöver installera kan du läsa Installera Azure PowerShell-modulen.

Kör Connect-AzAccount för att logga in på Azure.

Exempelskript

# 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

Rensa distribution

När skriptexemplet har körts kan följande kommando användas för att ta bort resursgruppen och alla resurser som är kopplade till den.

Remove-AzResourceGroup -ResourceGroupName "myResourceGroup"

Förklaring av skript

Det här skriptet använder följande kommandon. Varje kommando i tabellen länkar till kommandospecifik dokumentation.

Command Kommentar
Azure Cosmos DB
Get-AzCosmosDBAccount Visar en lista över Azure Cosmos DB-konton eller hämtar ett angivet Azure Cosmos DB-konto.
Get-AzCosmosDBMongoDBDatabase Listar API för MongoDB-databaser i ett konto eller hämtar ett angivet API för MongoDB Database i ett konto.
Get-AzCosmosDBMongoDBCollection Listar API för MongoDB-samlingar eller hämtar ett angivet API för MongoDB-samling i en databas.
Azure-resursgrupper
Remove-AzResourceGroup Tar bort en resursgrupp, inklusive alla kapslade resurser.

Nästa steg

Mer information om Azure PowerShell finns i Azure PowerShell-dokumentationen.