简体   繁体   中英

MongoDb C# Driver Resolving Command insert failed: document does not contain shard key

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 ?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM