How to read the Partitionkey of a container using Cosmosclient
See original GitHub issueOne of the partameters of CreateItemStreamAsync is the partionkey value in current message. How do we access the current container’s partition key?
I see ContainerPropeties has the PartitionKeyPath property, but Container class doesn’t have a Resource properlty https://learn.microsoft.com/en-us/dotnet/api/microsoft.azure.cosmos.containerproperties?view=azure-dotnet
Container container = await client.GetDatabase("dbName").Containers.CreateAsync("MyCollection", "/country", 50000);
ContainerProperties containerProperties = container.Resource;
Issue Analytics
- State:
- Created 6 months ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
c# - Programmatically find Partition Key Path of Cosmos ...
To retrieve the partition key of a container you have to use the GetContainerQueryIterator() method. Depending on what is already available ...
Read more >Read an item in Azure Cosmos DB for NoSQL using .NET
However, Azure Cosmos DB requires both the unique identifier and the partition key value of an item to perform a quick point read...
Read more >Creating a Partitioned Container with .NET SDK
Create CosmosClient Instance · Within the Program. · Locate the Program class and replace it with the following class: · Within the Program...
Read more >Azure | Lenni's Technology Blog
You need to define a partition key for your container, which is a property the every document contains. The property is used to...
Read more >Azure Cosmos DB Basics
You can query Cosmos DB using MongoDb API's , SQL Api , Graph API's and much more. ... You can not change the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hello @praveenbarli hopefully the following will help you with your problem.
A when creating a
Container
for ComsosDB, you must specify at minimum two things. First is theid
of the container. The second is thePartitionKeyPath
. ThePartitionKeyPathPath
tells Cosmos where to look for the partition key value of every item you add into a Cosmos Container. This path can have any value also long as the items you add to Cosmos have that property.For instance, let’s say you will be adding many of the following records to a Cosmos Database.
When creating the
Container
that will be used to store these items you can that any of these values are thePartitionKeyPath
. For example:or
are both valid (but not the only) ways to create the
Container
.Once a
Container
is created and we want to populate it with items we must make sure that the items have a property that matches theParitionKeyPath
we specified in the creation of theContainer
. Finding theParitionKeyPath
of aContainer
can be done like this:await container.ReadContainerAsync().PartitionKeyPath
. When using theCreateItemStreamAsync
method, we must specify two things. First is the stream containing the payload of the item we are creating, and second is theParitionKey
of that item.Let’s look at the following example that details the creation of three items using the
CreateItemStreamAsync
method:Here you can see the difference in what the
PartitonKeyPath
andPartitionKey
is. In both cases thePartitionKeyPath
stays the same,thisIsThePartitionKeyPath
, while the values of thePartitionKey
can change from item to item. However, as seen in the second and third items, they can have the same value. In summary, thePartitionKeyPath
tells Cosmos where to find the value of thePartitionKey
within an item and stays the same for eachContainer
, while thePartitionKey
is the actual value the items are partitioned on.Please let me know if you have any further questions, if not feel free to close this issue.
You should not call
.Result
- always useawait
to hop from aTask<X>
to anX
(you can search up an article on these sorts of things with.net Result await deadlock
)