Understanding how to mutate a document in Sanity.io

20 replies
Last updated: Jun 28, 2021
Hi! I'm trying to mutate a document and was reading the documentation on Sanity.io (Image). _id and _type in the createOrReplace, what should they be? The _id of the document? Is it the _type that it currently is, or the one I want to mutate it in to? Thank You!
Jun 28, 2021, 7:45 AM
every document has a unique id, do you know the id of the document you are mutating
Jun 28, 2021, 8:02 AM
also the type is image it sounds like unless you have created custom image document types
Jun 28, 2021, 8:03 AM
when you create a schema you define it's type
Jun 28, 2021, 8:03 AM
Yes, I have the Id. No it's not not an image. It's a document called staticPage that I want to mutate to be a type called removablePage
Jun 28, 2021, 8:05 AM
basically if you know the id already it will replace that document with the new details. if there isn't an id that matches then it will create the whole document
Jun 28, 2021, 8:05 AM
i'm actually not sure if you can mutate a type. maybe your best option there is update...
Jun 28, 2021, 8:06 AM
Okey! Hm...
Jun 28, 2021, 8:22 AM
the reason i think that is this finds what to update by the type and id. you need something that doesn't need the type to find the document to update
Jun 28, 2021, 8:23 AM
like this `
  .patch('bike-123') // Document ID to patch
  .set({inStock: false}) // Shallow merge
  .inc({numSold: 1}) // Increment field by count
  .commit() // Perform the patch and return a promise
  .then((updatedBike) => {
    console.log('Hurray, the bike is updated! New document:')
  .catch((err) => {
    console.error('Oh no, the update failed: ', err.message)

Jun 28, 2021, 8:25 AM
here you are patching just an id, perhaps you can set the type to something new
Jun 28, 2021, 8:25 AM
https://www.sanity.io/docs/js-client you'll need this library although i think that is also where you found mutate
Jun 28, 2021, 8:26 AM
Okey! Maybe you're right. I thought it would work because here is says that if the document changes type it will act as a delete and replace, which is what I want🤔
Jun 28, 2021, 8:29 AM
awww i see, then maybe it will work. try it
Jun 28, 2021, 8:31 AM
i created this when i first started with sanity https://github.com/SharonStrats/sanity-migration , it's not what I'm using now but perhaps it can help you some how.
Jun 28, 2021, 8:33 AM
I did, but with no result. But I'm not sure if the _type should be the type I want to change it to. Would make sense though. Maybe I'm not running the .js file correctly
Jun 28, 2021, 8:34 AM
the one thing I had to add was promises. i haven't updated this. but if you have a forEach you have to created a promises array and wait for promise.all(promises)
Jun 28, 2021, 8:34 AM
yes i agree it shoudl be what you want to change it to...
Jun 28, 2021, 8:35 AM
i agree completely
Jun 28, 2021, 8:35 AM
forEach ... awaiting your client.mutate....
Jun 28, 2021, 8:35 AM
Hi Emelie. The
field is immutable, so if you want to change it you would want to copy the document to a new
and then delete the old document. This can be automated with a script . The same rules apply for the
, which is also immutable.
Jun 28, 2021, 1:53 PM

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?