ComosDB - MongoAPI - Document does not contain shard key

does not contain shard key for pattern
mongodb shard key
shard key values
cosmos db mongodb shard key
mongodb compound shard key
uniqueness can t be maintained unless shard key is a prefix
query in command must target a single shard key
azure cosmos db 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".

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?

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)

Azure DevOps: Cosmos DB MongoError document does not contain , Create Build Pipeline using YAML in Azure DevOps This video covers the followings: 1. Explanation of Dot Net Core and Cosmos DB integration . 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.

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)

ComosDB - MongoAPI, When creating documents in Azure Cosmos databases for MongoDB API error message MongoError: document does not contain shard key. ComosDB - MongoAPI - Document does not contain shard key This issue comes in picture when we forget to put value for Shard Key while inserting values in document. It means once we have declared Shard key for document it should be part of model which is inserting. Kindly refer an images below:

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.

az cosmosdb collection create not same as in portal � Issue #8633 , 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� Hi, I have encountered an issue while trying to insert a document containing a shard key of type mongo ObjectId into a partitioned collection in the emulator. I'm using v1.22 of the emulator.

This issue comes in picture when we forget to put value for Shard Key while inserting values in document. It means once we have declared Shard key for document it should be part of model which is inserting. Kindly refer an images below:

Cosmosdb Emulator with mongo API cannot insert a shard key of , documents: query in command must target a single shard key" for each insert /comosdb-mongoapi-document-does-not-contain-shard-key. From the extension, try to add a document. Error: "document does not contain shard key" StephenWeatherford changed the title Can't create a document in a Mongo DB shard keys Can't create a document in a Mongo DB with shard keys on Mar 21, 2018 StephenWeatherford added this to the future milestone on Apr 12, 2018

I tried to fix the problem by creating a collection different ways mentioned here. In my case nothing helped me unfortunately. Eventually I modified this sample https://github.com/Azure-Samples/azure-cosmos-db-mongodb-dotnet-getting-started which created collecton automatically on insert.

ComosDB - MongoAPI, Cosmosdb Emulator with mongo API cannot insert a shard key of type ObjectId # 12911 "errmsg" : "document does not contain shard key", When using MongoDB csharp driver you should not call db.CreateCollection, but use sharding command instead. This will create your unlimitted collection with the partition key for you. //Sharded collection must be initialized this way var bson = new BsonDocument { { "shardCollection", mongoClientProvider.DatabaseName + "."

az cosmosdb collection create not same as in portal , I am investigating using CosmosDB (previously DocumentDB), we currently use MongoDB so I am trying to use the MongoAPI for CosmosDB. But when I try to insert a document from the UI in the portal nothing happens, the document hangs on inserting: Inserting a document from a C# library with the MongoB.Drivers nuget package also fails, getting the error: "document does not contain shard key" obviously my document contains the shard key property "TaskId".

关于c#:ComosDB-MongoAPI-文档不包含分片键| 码农家园, When using az cosmos collection create and --partition-key "<val>", the key insert you'll get "error inserting documents: document does not contain shard key" . Because the _id key is not part of the shard key, the collection could have a document with _id value 1 in shard A and another document with _id value 1 in shard B. If the _id field is not the shard key nor the prefix of the shard key, MongoDB expects applications to enforce the uniqueness of the _id values across the shards.

When using az cosmos collection create and --partition-key "<val>", the key created is /45587691/comosdb-mongoapi-document-does-not-contain-shard- key. In Azure Cosmos DB's API for MongoDB, sharding and indexing are separate concepts. You don't have to index your shard key. However, as with any other property in your document, if this property is a common filter in your queries, we recommend to index the shard key. To index additional fields, you apply the MongoDB index-management commands.

Comments
  • 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?
  • @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)
  • We'll have it fixed this week :)
  • 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!
  • 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.