在使用CLI创建的MongoDB分片集合中插入文档时出错
使用最新的Azureure CLI(2.0.21),我尝试在CosmosDB中使用以下脚本(从Powershell中)创建分片的MongoDB集合:
Using the latest azure cli (2.0.21) I am trying to create in CosmosDB a sharded MongoDB collection with the following script (from powershell) :
$resourceGroupName="RG-S01-APP-CASRWA-001"
$name="marcoolandesetest"
$databaseName="marco"
$collectionName="fromcli"
$originalThroughput=100000
$kind="MongoDB"
$partition="/TaskId"
az login
az cosmosdb create --name $name --kind $kind --resource-group
$resourceGroupName
az cosmosdb database create --name $name --db-name $databaseName --resource-
group $resourceGroupName
az cosmosdb collection create --collection-name $collectionName --name $name --db-name $databaseName --resource-group $resourceGroupName --throughput $originalThroughput --partition-key-path $partition --indexing-policy '@index.json'
index.json的内容如下:
The content of the index.json is the following:
{
"indexingMode": "consistent",
"automatic": true,
"includedPaths": [
{
"path": "/*",
"indexes": [
{
"kind": "Range",
"dataType": "Number",
"precision": -1
},
{
"kind": "Hash",
"dataType": "String",
"precision": 3
}
]
}
],
"excludedPaths": []
}
但是当我尝试从门户中的UI插入文档时,什么也没发生,文档在插入时挂起:
But when I try to insert a document from the UI in the portal nothing happens, the document hangs on inserting:
使用MongoB.Drivers nuget包从C#库插入文档也失败,并显示错误:
Inserting a document from a C# library with the MongoB.Drivers nuget package also fails, getting the error:
文档不包含分片键"
很明显,我的文档包含分片属性"TaskId" .
如果我从Portal中的UI创建相同类型的集合,则在C#中,也可以使用相同的代码正常运行
以下是集合设置的两个屏幕截图,如您所见,它们是相同的:
Here are two screenshots of the settings of the collections, as you can see they are the same:
集合"fromui"是从门户网站UI创建的,使用此工具,一切正常,从门户网站插入文档,以及从C#插入.
Collection "fromui" is created from the portal UI, and with this one everything works fine, inserting a document from the portal, and inserting from C# as well.
集合"fromcli"是使用上面的CLI脚本创建的,并插入了一个根本无法使用的文档.
Collection "fromcli" is created using the CLI script above and inserting a document it's not working at all.
Microsoft确认CLI(2.0.21)中存在错误,他们将很快推出更新.
Microsoft confirmed there is a bug in the CLI (2.0.21), they will soon roll out an update.
我创建了一个博客文章来描述如何解决此问题,直到发布具有修复程序的新CLI版本:
I created a blog post to describe how to solve this until a new CLI version will be release with a fix:
https://blog.olandese.nl/2017/12/13/create-a-sharded-mongodb-in-azure-cosmos-db/