2

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??

3
  • 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 cetera Commented Aug 16, 2018 at 11:22
  • 1
    Why are you instantiating $document as String? You could just use ConvertTo-Json. It helps you to ensure, your json is valid. Commented Aug 16, 2018 at 11:24
  • good suggestion! not any specific reason. Commented Aug 16, 2018 at 12:45

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Are you able to insert data?? I already tried this (I updated my question with the complete command) but still getting 400 error.
Why you used $partitionkey = "A" If partitionKey attribute name is RuleType??
Yes,I inserted data successfully.In you situation,please use $partitionkey = "FTOD"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.