Unlock seamless workflows and faster delivery with our latest releases - Join the deep dive

How to use a Patch Query to Unset an Array Element

8 replies
Last updated: Apr 2, 2022
I can't understand patch query where i want to unset an array element . Can anyone help me with it. Just post an example query please.
Apr 1, 2022, 8:22 AM
Based on its value
Apr 1, 2022, 12:06 PM
Can you help me with it please I can't find any solution.
Apr 1, 2022, 12:07 PM
user E
based on value
Apr 1, 2022, 12:09 PM
You can do this by passing a JSONMatch expression to the unset operation, for example:

unset: ['@ == "myValue"']
This would unset any item with the value "myValue".

You can also target items based on nested values:


unset: ['person.id == 13']

We have some documentation about the _JSONMatch _ syntax here .
Apr 1, 2022, 1:26 PM
user E

{
  "_createdAt": "2022-03-30T05:34:13Z",
  "_id": "Ein4V1AQOMSZUazhbENMD4",
  "_rev": "LsWDvItnjEcQW0nZ8HMFDw",
  "_type": "pin",
  "_updatedAt": "2022-03-30T17:08:24Z",
  "about": "Two cats in a lovely basket with white background",
  "category": "cats",
  "destination": "Islamabad, Rawalpindi",
  "image": {
    "_type": "image",
    "asset": {
      "_ref": "image-a3b1d874e42957c1d0259564df103b8420e198e7-640x427-jpg",
      "_type": "reference"
    }
  },
  "postedBy": {
    "_ref": "112412412793415669744",
    "_type": "postedBy"
  },
  "price": 7000,
  "save": [
    {
      "_key": "67f3bb67-0220-4b04-9b5b-fa76e2dc9e39",
      "postedBy": {
        "_ref": "112412412793415669744",
        "_type": "postedBy"
      },
      "userId": "112412412793415669744"
    },
    {
      "_key": "6ed25a89-a355-4460-b8c4-b257adfbbdc9",
      "postedBy": {
        "_ref": "114715636637497218934",
        "_type": "postedBy"
      },
      "userId": "114715636637497218934"
    }
  ],
  "title": "Twin Cats",
  "userId": "112412412793415669744"
}
I want to unset elements of save can you please tell me the whole query . I would be very grateful
Apr 1, 2022, 6:12 PM
based on userId
Apr 1, 2022, 6:13 PM
Thanks that worked.
Apr 2, 2022, 4:55 AM
Thanks that worked.
Apr 2, 2022, 4:55 AM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?