2
votes

I'm getting an error when I am trying to insert data in Cosmos DB collection having Partition Key.

Without Partition key, it's working fine

$resourceGroupName = "myrg"
$cosmosDbAccountName = "mydb"
$databaseName = "test"

$cosmosDbContext = New-CosmosDbContext -Account $cosmosDbAccountName -        
Database $databaseName -ResourceGroup $resourceGroupName    

New-CosmosDbCollection -Context $cosmosDbContext -Id 'events' -OfferThroughput 1000 -PartitionKey 'RuleType' -DefaultTimeToLive 604800

$document = @"
{
        "id": "$([Guid]::NewGuid().ToString())",
        "createTime": "2018-05-21T22:59:59.999Z",
        "RuleType": "FTOD"
    }

"@
New-CosmosDbDocument -Context $cosmosDbContext -CollectionId 'events' - 
DocumentBody $document  -PartitionKey 'RuleType'

"RuleType": This is my Partition Key

enter image description here

Invoke-WebRequest : The remote server returned an error: (400) Bad Request. At C:\Program Files\WindowsPowerShell\Modules\CosmosDB\2.1.4.536\lib\utils.ps1:554 char:30 + ... estResult = Invoke-WebRequest -UseBasicParsing @invokeWebRequestParam ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

Anyone know about this??

1
I would really suggest you to add your error message as text, not as an image. It makes it easier to read, copy, evaluate, search for, et ceteraClijsters
Why are you instantiating $document as String? You could just use ConvertTo-Json. It helps you to ensure, your json is valid.Clijsters
good suggestion! not any specific reason.Pankaj Rawat

1 Answers

2
votes

I reproduced your issue on my side.

enter image description here

Based on parameter list which Get-Help New-CosmosDbDocument command shows:

enter image description here

You need to add partitionkey and you could insert document successfully.

$resourceGroupName = "***"
$cosmosDbAccountName = "***"
$databaseName = "db"

$cosmosDbContext = New-CosmosDbContext -Account $cosmosDbAccountName -Database $databaseName -ResourceGroup $resourceGroupName    

$document = @"
{
        "id": "6fa9b3d5-ce1a-4b38-9068-9d17de5b1c69",
        "createTime": "2018-05-21T22:59:59.999Z",
        "RuleType": "FTOD"           
    }

"@
$partitionkey = "FTOD"
New-CosmosDbDocument -Context $cosmosDbContext -CollectionId 'part' -DocumentBody $document -PartitionKey $partitionkey

Hope it helps you.