简体   繁体   中英

Partition Key for Cosmos DB

I am building an restful API using ASP.NET Core with Cosmos DB. There's a GetItemById request that needs a partition key.

 public static async Task<T> GetItemAsync(string itemId, string name)
    {
        try
        {
            Document document =
                await client.ReadDocumentAsync(UriFactory.CreateDocumentUri(DatabaseId, CollectionId, itemId),
                    new RequestOptions() { PartitionKey = new PartitionKey(name) });

            return (T)(dynamic)document;
        }
        catch (DocumentClientException e)
        {
            if (e.StatusCode == System.Net.HttpStatusCode.NotFound)
            {
                return null;
            }
            else
            {
                throw;
            }
        }
    }

The GetByIdAsync() function calls this GetItemAsync() method.

    [HttpGet("{itemId}")]
    public async Task<IActionResult> GetByIdAsync(string itemId, string name)
    {
        var item = await DocumentDBRepository<TodoItem>.GetItemAsync(itemId, name);
        if (item == null)
        {
            return NotFound();
        }
        return new ObjectResult(item);
    }

The URL would be http://localhost:54084/api/todo/f323307b-142f-46f3-9072-12f41cc74e87

My Azure CosmosDB container looks like this: 在此处输入图片说明

But when I run this, it gave me an error with

{Microsoft.Azure.Documents.DocumentClientException: Partition key provided either doesn't correspond to definition in the collection or doesn't match partition key field values specified in the document.

In Cosmos DB, the primary key is the combination of partition key and the row key ("id"). The combination of the two uniquely identifies a row - not the "id" alone. So you need to specify the value of Name in the partition key to find the item (not null).

If your app doesn't have a natural PK, then you should consider setting "/id" as the partition key (and pass the value for both id and partition key)

I figured it out. The reason why I got this error is because I didn't have partition key set up in my collection. That's why I should remove

new RequestOptions() { PartitionKey = new PartitionKey(name) }

After I remove it, it is working properly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM