Search code examples
nosqlazure-cosmosdbunique-key

Cosmos DB unique key


It is possible to create unique constraint on property in subcollection in json? I have json like that:

{
"id":111,
"DataStructs":[
   "Name": "aaaaa",
   "DataStructCells": [
      {
          "RowName": "Default",
          "ColumnName": "Perfect / HT",
          "CellValue": "0.1"
       },
       {
          "RowName": "Default",
          "ColumnName": "100% / HT",
          "CellValue": "0.2"
       }
    ]
]
}

And I wanted to add unique key to prevent to add two the same RowName: Default When I create collection I added unique key: /DataStructs/DataStructCells/RowName but it doesn't work.


Solution

  • No, that is impossible. Unique keys can only work with different documents within a logic partition. To achieve your requirement, you can split your array to different documents in a same logic partition. You can refer to this How do I define unique keys involving properties in embedded arrays in Azure Cosmos DB?