问题
I've created an Azure Cosmos DB (selecting MongoDB as the language), and while creating a new document through the Azure portal (website) it fails and says:
{"code":400,"body":"Command insert failed: document does not contain shard key."}
I tried to create the following document through the website:
{
"wordpress_id": "test"
}
And this:
{
"id" : "replace_with_new_document_id",
"wordpress_id": "test"
}
"wordpress_id"
is a "Shard key"
(or partition key) that I defined when I created the collection. It is also a Unique key.
This is what I see in the "Scale & Settings" tab. This is the error I see when I try to create the document.
Am I missing something?
Do you know why this is happening?
回答1:
Am I missing something? Do you know why this is happening?
I also could reproduce this issue if I created a shared collection from Azure portal.
According to the azure official document, we could know that we could create the shared collection with favorite tool,driver, or SDK.
With the MongoDB API, you can create a sharded collection through your favorite tool, driver, or SDK.
I do a demo with studio3t tool. The Mongo Shell I used
db.runCommand( { shardCollection: "databaseName.collectionName", key: { KeyName: "hashed" } } )
Run the Mongo Shell from the studio3t
After that we could create the document from Azure portal correctly.
来源:https://stackoverflow.com/questions/48188495/cosmos-db-error-code-400-while-creating-a-new-document