次の方法で共有


Azure Cosmos DB 用のデータベースとコレクションを作成する - MongoDB 用 API

適用対象: MongoDB

Note

Azure を操作するには、Azure Az PowerShell モジュールを使用することをお勧めします。 作業を開始するには、「Azure PowerShell のインストール」を参照してください。 Az PowerShell モジュールに移行する方法については、「AzureRM から Az への Azure PowerShell の移行」を参照してください。

このサンプルには、Azure PowerShell Az 5.4.0 以降が必要です。 Get-Module -ListAvailable Az を実行して、インストールされているバージョンを確認します。 インストールする必要がある場合は、Azure PowerShell モジュールのインストールに関するページを参照してください。

Connect-AzAccount を実行して Azure にサインインします。

サンプル スクリプト

# Reference: Az.CosmosDB | https://docs.microsoft.com/powershell/module/az.cosmosdb
# --------------------------------------------------
# Purpose
# Create Cosmos MongoDB API account with automatic failover,
# a database, and a collection with dedicated throughput.
# --------------------------------------------------
Function New-RandomString{Param ([Int]$Length = 10) return $(-join ((97..122) + (48..57) | Get-Random -Count $Length | ForEach-Object {[char]$_}))}
# --------------------------------------------------
$uniqueId = New-RandomString -Length 7 # Random alphanumeric string for unique resource names
$apiKind = "MongoDB"
# --------------------------------------------------
# Variables - ***** SUBSTITUTE YOUR VALUES *****
$locations = @()
$locations += New-AzCosmosDBLocationObject -LocationName "East Us" -FailoverPriority 0 -IsZoneRedundant 0
$locations += New-AzCosmosDBLocationObject -LocationName "West Us" -FailoverPriority 1 -IsZoneRedundant 0

$resourceGroupName = "myResourceGroup" # Resource Group must already exist
$accountName = "cosmos-$uniqueId" # Must be all lower case
$serverVersion = "4.2" #3.2, 3.6, 4.0, or 4.2
$consistencyLevel = "Session"
$tags = @{Tag1 = "MyTag1"; Tag2 = "MyTag2"; Tag3 = "MyTag3"}
$databaseName = "myDatabase"
$collectionName = "myCollection"
$collectionRUs = 400
$shardKey = "user_id"
$partitionKeys = @("user_id", "user_address")
$ttlKeys = @("_ts")
$ttlInSeconds = 604800
# --------------------------------------------------
Write-Host "Creating account $accountName"
$account = New-AzCosmosDBAccount -ResourceGroupName $resourceGroupName `
    -LocationObject $locations -Name $accountName -ApiKind $apiKind -Tag $tags `
    -DefaultConsistencyLevel $consistencyLevel `
    -EnableAutomaticFailover:$true -ServerVersion $serverVersion

Write-Host "Creating database $databaseName"
$database = New-AzCosmosDBMongoDBDatabase -ParentObject $account `
    -Name $databaseName

$index1 = New-AzCosmosDBMongoDBIndex -Key $partitionKeys -Unique $true
$index2 = New-AzCosmosDBMongoDBIndex -Key $ttlKeys -TtlInSeconds $ttlInSeconds
$indexes = @($index1, $index2)

Write-Host "Creating collection $collectionName"
$collection = New-AzCosmosDBMongoDBCollection -ParentObject $database `
    -Name $collectionName -Throughput $collectionRUs `
    -Shard $shardKey -Index $indexes

デプロイのクリーンアップ

スクリプト サンプルの実行後は、次のコマンドを使用してリソース グループとすべての関連リソースを削除することができます。

Remove-AzResourceGroup -ResourceGroupName "myResourceGroup"

スクリプトの説明

このスクリプトでは、次のコマンドを使用します。 表内の各コマンドは、それぞれのドキュメントにリンクされています。

コマンド Notes
Azure Cosmos DB
New-AzCosmosDBAccount Azure Cosmos DB アカウントを作成します。
New-AzCosmosDBMongoDBDatabase MongoDB 用 API のデータベースを作成します。
New-AzCosmosDBMongoDBIndex MongoDB 用 API のインデックスを作成します。
New-AzCosmosDBMongoDBCollection MongoDB 用 API のコレクションを作成します。
Azure リソース グループ
Remove-AzResourceGroup 入れ子になったリソースすべてを含むリソース グループを削除します。

次のステップ

Azure PowerShell の詳細については、Azure PowerShell のドキュメントを参照してください。