3

I am experimenting with MongoDB Api for CosmosDB and I have the following error while inserting a value in the database :

Command insert failed: document does not contain shard key.

I'm using cosmosdb emulator, My Partition Key is "Id" Here is a simple insert I tried :

        mongoCollection.InsertOne(new { Name = "John", Id = 007 });

To resolve this issue, I searched and saw this question :

Similar to mine,

I tried running the command specified in the answer to the question, as follows :

        var command = new BsonDocument("{ shardCollection: \"Mydb.collection1\", key: { Id: \"hashed\" } }", 1);
        database.RunCommand<BsonDocument>(command);

But it doesn't work and throws me another exception, Please can someone tell me what to do ?

Damien Doumer
  • 1,991
  • 1
  • 18
  • 33

1 Answers1

0

I tried to fix the problem by creating a collection different ways via azure portal, shell, programmatically. Nothing helped me. Eventually I modified this sample https://github.com/Azure-Samples/azure-cosmos-db-mongodb-dotnet-getting-started which created collecton automatically on insert.

Daniil Grankin
  • 3,841
  • 2
  • 29
  • 39