John von No Man
John von No Man

Reputation: 3030

Create a cross-partition unique constraint in Cosmos DB

I'm working with a Cosmos DB collection partitioned by account ID, and I need to prevent the addition of duplicate serial numbers.

So, if Johnny with account ID 123 tries to add an item with serial number A562, Bill with an account ID of 987 can't add an item with serial number A562. However, they may not be in the same partition.

Is there a way to ensure uniqueness across partitions without doing an expensive search through every partition first?

Upvotes: 2

Views: 1038

Answers (1)

Jay Gong
Jay Gong

Reputation: 23782

Based on the official doc Unique keys in Azure Cosmos DB : By creating a unique key policy when a container is created, you ensure the uniqueness of one or more values per partition key.

So the unique key can not ensure uniqueness across partitions so far.

I suggest you use sql SELECT VALUE COUNT(1) FROM c where c.serialNumber = '***' to check if the result is larger than 2.

Hope it helps you.

Upvotes: 4

Related Questions