简体   繁体   English

MongoDb C#驱动程序解析命令插入失败:文档不包含分片键

[英]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 : 我正在尝试使用MongoDB Api for CosmosDB,并且在数据库中插入值时出现以下错误:

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 : 我正在使用cosmosdb模拟器,我的分区键是“Id”这是一个我试过的简单插入:

        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. 我尝试通过azure portal,shell以编程方式创建不同方式的集合来解决问题。 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. 最后我修改了这个示例https://github.com/Azure-Samples/azure-cosmos-db-mongodb-dotnet-getting-started ,它在插入时自动创建了collecton。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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