使用最新的 azure cli (2.0.21),我尝试使用以下脚本(来自 powershell)在 CosmosDB 中创建分片 MongoDB 集合:
$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的内容如下:
{
"indexingMode": "consistent",
"automatic": true,
"includedPaths": [
{
"path": "/*",
"indexes": [
{
"kind": "Range",
"dataType": "Number",
"precision": -1
},
{
"kind": "Hash",
"dataType": "String",
"precision": 3
}
]
}
],
"excludedPaths": []
}
但是,当我尝试从门户中的 UI 插入文档时,没有任何反应,文档在插入时挂起:
使用 MongoB.Drivers nuget 包从 C# 库插入文档也会失败,并出现错误:
“文档不包含分片键”
显然我的文档包含分片键属性“TaskId”.
如果我从门户中的 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 脚本创建的,并插入一个根本不起作用的文档。