0

I have a container with id as partition key. Based on some condition, I do not want to enter duplicate data in my container, but I am not sure how to do that in Cosmos. i.e., I tried to create unique keys, but that didn't help me.

Condition:

Record will be duplicate if name + addresses[].city + addresses[].state + addresses[].zipCode are same.

Document:

{
    "isActive": false,
    "id": "d94d7350-8a5c-4300-b4e4-d4528627ffbe",
    "name": "test name",
    "addresses": [
        {
            "address1": "718 Old Greenville Rd",
            "address2": "",
            "city": "Montrose",
            "state": "PA",
            "zipCode": "18801",
            "audit": {}
        }
    ]
}

Findings:

Questions:

  • Do I need to change partition key? Not sure if I can have /id#name (or something like that) in Cosmos like Dynamo?
  • Is there any other way of handling this at DB level?

As a last resort, I can add the logic in my code to do this but that would be expensive in terms of RU/s.

GThree
  • 2,708
  • 7
  • 34
  • 67
  • 1
    You'll need to figure out how to include your unique content in a non-array property. Uniqueness in an array doesn't really work anyway, since your array can grow infinitely (well, up until the maximum document size), and you would need to re-check for uniqueness with every addition to your array (across all arrays in every document, every time you added to an array). – David Makogon Jul 05 '22 at 16:57
  • 2
    And if you really want addresses to be unique, then you should consider storing each address as its own document (where you can now have a composite key across various properties), and then reference such addresses within your main document (e.g. an array of address document id's). – David Makogon Jul 05 '22 at 16:58
  • @DavidMakogon your second comment makes sense. Let me see what I can do here. Thank you. – GThree Jul 05 '22 at 18:22

0 Answers0