简体   繁体   English

ComosDB-MongoAPI-文档不包含分片键

[英]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. 我正在使用CosmosDB(以前是DocumentDB)进行调查,我们目前使用MongoDB,因此我试图将MongoAPI用于CosmosDB。

I have created a CosmosDB deployment in azure, created a collection and specified a partition key of "/rateId". 我已经在azure中创建了一个CosmosDB部署,创建了一个集合,并指定了“ / 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: 据我从Microsoft的文档可以理解,此分区键应与我插入的每个文档中的一个属性有关,因此我尝试插入一个基本文档,如下所示:

{
    "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". 但是,当我尝试插入此文件(通过Mongo C#驱动程序或通过MongoChef)时,出现错误“文档不包含分片键”。

在此处输入图片说明

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 微软在文档中说使用此命令通过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). 我用它来创建一个集合,它现在可以按预期的方式工作(具有rateId属性的文档插入ok,而没有得到“ no shard key”错误)。

When looking at the collection in the Azure Portal it shows the shard key as 在Azure门户中查看集合时,分片密钥显示为

$v.rateId.$v

Whereas when I created the collection through the portal and specified /rateId as the partition, it showed it as just 而当我通过门户网站创建集合并指定/ rateId作为分区时,它显示为

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) 至少我现在可以进步了,但是我很困惑为什么它会这样运行,或者这就是它的原意(我在文档中看不到这种“ $ v”格式的提及)

By playing with escape chars I managed to find out how to write partition-key-path using az cli 通过玩转义字符,我设法找到了如何使用az cli编写partition-key-path的方法。

Used AZ CLI Version 2.0.59 二手AZ CLI版本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 (ie "/foo") 其中path是您在文档中以斜杠(即“ / foo”)开头的路径

BTW: This should be working in previous AZ CLI versions (see: https://github.com/Azure/azure-cli/issues/8633 ) 顺便说一句:这应该在以前的AZ CLI版本中起作用(请参阅: https : //github.com/Azure/azure-cli/issues/8633

I ended up here with the same error message. 我在这里结束了同样的错误消息。 Azure CosmosDB, MongoDB api. Azure CosmosDB,MongoDB api

If I create the collection with the Azure CLI, I get the error. 如果使用Azure CLI创建集合,则会收到错误消息。

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. 另一方面,如果我使用接受的答案中提到的命令来创建集合( db.runCommand( { shardCollection: "myDb.myCollection", key: { rateId: "hashed" } } ) ),则错误消失了。

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 (eg sender.postCode ) 通过MongoDB创建集合,指定用点分隔的分区键路径(例如sender.postCode
  2. use the AZ CLI to update it with the desired throughput 使用AZ CLI以所需的吞吐量更新它

In any case, the partitioning always seems to work from Azure's portal. 无论如何,该分区似乎总是可以从Azure的门户进行工作。

The $v prefix mentioned here is no longer present anywhere. 这里提到的$v前缀不再出现在任何地方。

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. 这意味着一旦我们为文档声明了Shard键,它就应该是要插入的模型的一部分。 Kindly refer an images below: 请参考以下图片:

在此处输入图片说明

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. 最终,我修改了此样本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