问题
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'
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": []
}
But when I try to insert a document from the UI in the portal nothing happens, the document hangs on inserting:
Inserting a document from a C# library with the MongoB.Drivers nuget package also fails, getting the error:
"document does not contain shard key"
obviously my document contains the shard key property "TaskId".
If I create the same kind of collection from the UI in the Portal everything works fine, also in C#, using the same code
Here are two screenshots of the settings of the collections, as you can see they are the same:
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.
Collection "fromcli" is created using the CLI script above and inserting a document it's not working at all.
回答1:
Microsoft confirmed there is a bug in the CLI (2.0.21), they will soon roll out an update.
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/
回答2:
Which browser do you use to create new document in the Portal? Could you please try to use another browser (Edge, Firefox, Chrome, Safari) or clean the browser cache completely and retry?
来源:https://stackoverflow.com/questions/47591635/error-inserting-a-document-in-a-mongodb-sharded-collection-created-with-the-cli