ComosDB - MongoAPI - Document does not contain shard key

10,499

Solution 1

In the documentation Microsoft say to use this command for creating a collection through the mongo shell

db.runCommand( { shardCollection: "myDb.myCollection", key: { rateId: "hashed" } } )

I used that to create a collection and it now works as expected (docs with a rateId property insert ok, without I get the "no shard key" error).

When looking at the collection in the Azure Portal it shows the shard key as

$v.rateId.$v

Whereas when I created the collection through the portal and specified /rateId as the partition, it showed it as just

rateId

At least I can progress now, but I'm confused why it behaves this way or if this is how it's meant to be (I can't see any mention of this "$v" format on the documentation)

Solution 2

I ended up here with the same error message. Azure CosmosDB, MongoDB api.

If I create the collection with the Azure CLI, I get the error.

If on the other hand I create the collection with the command mentioned in the accepted answer (db.runCommand( { shardCollection: "myDb.myCollection", key: { rateId: "hashed" } } ) ) then the error goes away.

This means that in order to provision a collection with a partitioned key, I need to:

  1. create the collection via MongoDB, specifying the partition key path separated by dots (e.g. sender.postCode)
  2. use the AZ CLI to update it with the desired throughput

In any case, the partitioning always seems to work from Azure's portal.

The $v prefix mentioned here is no longer present anywhere.

Solution 3

By playing with escape chars I managed to find out how to write partition-key-path using az cli

Used AZ CLI Version 2.0.59

$paritionKeyPath = '/''$v''' + $path + '/''$v'''
az cosmosdb collection create .... --partition-key-path $partitionKeyPath

Where path is Your path in document starting with slash (i.e. "/foo")

BTW: This should be working in previous AZ CLI versions (see: https://github.com/Azure/azure-cli/issues/8633)

Share:
10,499

Related videos on Youtube

QTom
Author by

QTom

Updated on June 04, 2022

Comments

  • QTom
    QTom about 2 years

    I am investigating using CosmosDB (previously DocumentDB), we currently use MongoDB so I am trying to use the MongoAPI for CosmosDB.

    I have created a CosmosDB deployment in azure, created a collection and specified a partition key of "/rateId".

    As far as I can understand from Microsofts documentation this partition key should relate to a property in each document I insert, so I am trying to insert a basic document like so:

    {
        "rateId": "test.1",
        "val": "test2"
    }
    

    However when I try to insert this (through Mongo C# driver or through MongoChef) I get an error "document does not contain shard key".

    enter image description here

    I have tried this every which way I can think of and every time I am denied with this error. Am I misunderstanding how this is meant to work, or doing something wrong?

  • Matias Quaranta
    Matias Quaranta almost 7 years
    Hi Tom. Could you specify where exactly are you seeing $v.rateId.$v in the Portal? Is it in the Data Explorer? In the Scale & Settings subsection?
  • QTom
    QTom almost 7 years
    @MatiasQuaranta Yes, it was in the Data Explorer under Scale & Settings, it was also shown under the Documents tab alongside id (although there it also had some escaped characters I think)
  • Astrid
    Astrid over 6 years
    We experienced the same problem in a Scala application. We use az cosmosdb collection create on the command line to set up Cosmos, and the argument for --partition-key-path had to be "/'\$v'${PARTITION_KEY_PATH}/'\$v'" instead of "${PARTITION_KEY_PATH}" in order to work. I'd love to know what the $v is about too!
  • Code Junkie
    Code Junkie over 5 years
    Is this still an issue? i'm using the Azure CLI to create a collection and partition key and i'm getting the same issue.