Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ComosDB - MongoAPI - Document does not contain shard key

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?

like image 658
QTom Avatar asked Aug 09 '17 10:08

QTom


People also ask

What is shard key in azure cosmos DB?

For sharded collections, you must provide the shard (partition) key to create a unique index. In other words, all unique indexes on a sharded collection are compound indexes where one of the fields is the partition key.

What is shard key in MongoDB?

The shard key is either a single indexed field or multiple fields covered by a compound index that determines the distribution of the collection's documents among the cluster's shards.

What is sharding in Cosmos DB?

An advanced and powerful feature is the ability to partition data. This is commonly referred to as sharding. Azure Cosmos DB manages this for you when you follow these prerequisites for partitioning.

Does Cosmos use sharding?

Cosmos implements sharding using zones. It's a sharding solution that preserves the sovereignty of the shard as a self-governing and self-validating system.


2 Answers

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.

like image 152
Nikolaos Georgiou Avatar answered Oct 09 '22 09:10

Nikolaos Georgiou


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)

like image 25
QTom Avatar answered Oct 09 '22 08:10

QTom