Understanding how to mutate a document in Sanity.io
20 replies
Last updated: Jun 28, 2021
E
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
S
every document has a unique id, do you know the id of the document you are mutating
Jun 28, 2021, 8:02 AM
S
also the type is image it sounds like unless you have created custom image document types
Jun 28, 2021, 8:03 AM
S
when you create a schema you define it's type
Jun 28, 2021, 8:03 AM
E
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
S
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
S
i'm actually not sure if you can mutate a type. maybe your best option there is update...
Jun 28, 2021, 8:06 AM
E
Okey! Hm...
Jun 28, 2021, 8:22 AM
S
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
S
like this `
client .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:') console.log(updatedBike) }) .catch((err) => { console.error('Oh no, the update failed: ', err.message) })
Jun 28, 2021, 8:25 AM
S
here you are patching just an id, perhaps you can set the type to something new
Jun 28, 2021, 8:25 AM
S
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
E
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
S
awww i see, then maybe it will work. try it
Jun 28, 2021, 8:31 AM
S
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
E
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
S
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
S
yes i agree it shoudl be what you want to change it to...
Jun 28, 2021, 8:35 AM
S
i agree completely
Jun 28, 2021, 8:35 AM
S
forEach ... awaiting your client.mutate....
Jun 28, 2021, 8:35 AM
G
Hi Emelie. The
_typefield is immutable, so if you want to change it you would want to copy the document to a new
_typeand then delete the old document. This can be automated with a script . The same rules apply for the
_id, which is also immutable.
Jun 28, 2021, 1:53 PM
Sanity– build remarkable experiences at scale
The Sanity Composable Content Cloud is the headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.