I am trying to add a new field to a CosmosDB item. The Field i want to add is an object. For example:
jobResult = { description: "exampledescription", value: "examplevalue" }
I tried the following code:
... const { CosmosClient } = require("@azure/cosmos"); const endpoint = "VALUE"; const key = "VALUE"; const client = new CosmosClient({ endpoint, key }); const { database } = await client.databases.createIfNotExists({ id: 'VALUE' }); const { container } = await database.containers.createIfNotExists({ id: 'VALUE' }); const operation = [ { op: "Add", path: "/result", value: jobResult } ]; const { resource: updated } = await container.item(id = '42', partitionKeyValue = '/id') .patch(operation); ...
I get the error:
Entity with the specified id does not exist in the system
The item exists because i get the item when using container.item(“42”).read()
Advertisement
Answer
The value you have provided for the partitionKeyValue
is incorrect.
The code you are currently running is looking for an item with an id of 42
in a container partition of /Id
. Instead of providing the path, you will need to provide the value of the partition key, which is set during the creation of the container.
From the looks of your container creation, however, it looks like there is no partitionKey path set, so you shouldn’t need to provide it:
... const { resource: updated } = await container.item(id = '42') .patch(operation); ...
If you wanted to set the partitionKey, then you would want your container creation to look like this:
... const { container } = await database.containers.createIfNotExists({ id: "VALUE", partitionKey: "/Id", }); ...
Note: you will likely have to delete your container so that it is recreated with the partition key set.