Home > Back-end >  Cosmos DB Increment with Partial Document Update
Cosmos DB Increment with Partial Document Update

Time:07-20

https://docs.microsoft.com/en-us/azure/cosmos-db/partial-document-update#supported-operations says that I can use "increment" to increment a value in my database, however this code

const operations =
          [
              { op: 'increment', path: '/public/inventory/irradish', value:  irradish},
              { op: 'increment', path: '/public/inventory/sunmelon', value:  sunmelon},
              { op: 'set', path: '/map/garden/lastUse', value:  time}
          ];
          const { resource: updated } = await container
          .item(
              id = email, 
              partitionKeyValue = email
          )

throws this error:

Error: Message: {"Errors":["Invalid value of the patch operation 'op' property in patch request: 'increment'"]} ActivityId: f1bb8b88-ab68-4f2b-b244-8b021b0d9021,...

How do I increment?

CodePudding user response:

The way to do it is to use "incr" instead of "increment".

I requested the documentation be updated to reflect this.

CodePudding user response:

You need to use "incr" as answered above.

However regarding the documentation, increment is just an operation. The samples in the Node SDKenter link description here repository illustrates how to do increment operation,

const multipleOperations: PatchOperation[] = [
      {
        op: "add",
        path: "/aka",
        value: "MeFamily",
      },
      {
        op: "replace",
        path: "/lastName",
        value: "Jose",
      },
      {
        op: "remove",
        path: "/parents",
      },
      {
        op: "set",
        path: "/address/zip",
        value: 90211,
      },
      {
        op: "incr",
        path: "/address/zip",
        value: 5,
      },
    ];
    const { resource: patchSource2 } = await container.item(patchId!).patch(multipleOperations);
    if (patchSource2) {
      console.log(`Patched ${JSON.stringify(patchSource)} to new ${JSON.stringify(patchSource2)}.`);
    }
  • Related