Azure DevOps: Cosmos DB MongoError document does not contain shard key

When creating documents in Azure Cosmos databases for MongoDB API you might get the error message MongoError: document does not contain shard key.

The issue occurs for partitioned collections that have been created via the Azure CLI, because of the way the partitionKey path is being stored in the collection settings.

Reproduction steps

1. From the Azure Portal, create a Cosmos DB account with the Azure Cosmos DB for MongoDB API.

Account Name: xyzreprodbaccount

API: Azure Cosmos DB for MongoDB API

2. Create a database.

Database name: xyzreprodatabase

3. Create a collection with a shard key.

Collection id: collection1

Shard key: tid

4. Create a new document and save it.

a. Browse to Data Explorer > Database > Collection > Documents.

b. Click on New Document.

c. In the editor type in the following text:

{
     "id" : "1",
     "tid": "aaa"
}

d. Click Save.

e. Observe that the document has been created.

5. Create another collection using the Azure CLI.

You can create the collection also from the Azure Shell (https://shell.azure.com/). Use the following command:

$partitionKeyPath = '/tid'

az cosmosdb collection create --resource-group-name XYZ-repro --name xyzreprodbaccount --db-name xyzreprodatabase --collection-name collection2 --throughput 400 --partition-key-path $partitionKeyPath 

6. From the Azure Portal, browse the collection2 created at step 5 and insert a new document.

a. Browse to Data Explorer > Database > Collection > Documents.

b. Click on New Document.

c. In the editor type in the following text:

{
     "id" : "1",
     "tid": "aaa"
}

d. Click Save.

Expected result: the document to be saved

Actual result: error message “Command insert failed: document does not contain shard key”

When saving a document from an application, the error message is “MongoError: document does not contain shard key”.

Problem analysis

Looking at the collection properties using the command

az cosmosdb collection show --resource-group-name XYZ-repro --name xyzreprodbaccount --db-name xyzreprodatabase --collection-name collection1

we can see that for the first collection created from the Azure Portal, the path for the partitionKey is stored as:

"partitionKey": {
      "kind": "Hash",
      "paths": [
        "/'$v'/tid/'$v'"
      ]
    } 

While for the second collection created via Azure CLI, the path for the partitionKey is stored as:

"partitionKey": {
      "kind": "Hash",
      "paths": [
        "/tid"
      ]
    } 
Workaround

When creating collections via Azure CLI, specify the partition key as /’$v’/tid/’$v’

Here is the AZ CLI command:

$partitionKeyPath = '/tid'
$Bugfix_partitionKeyPath = '/''$v''' + $partitionKeyPath + '/''$v'''

az cosmosdb collection create --resource-group-name XYZ-repro --name xyzreprodbaccount --db-name xyzreprodatabase --collection-name collection2 --throughput 400 --partition-key-path $Bugfix_partitionKeyPath 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s