How to Set New Values on Types when Migrating Datasets

3 replies
Last updated: Apr 26, 2020
Question about migrating data - this example is good ( ). It unsets a document's
and sets a
. But let's say for something more complicated
My document has a
of block content and for example I want to look at all the blocks inside each document body and find the ones that are
_type == 'ctaButton'
and change them to a new type
_type = 'newCtaButton'
when I change it to the new type I also want to set some new values that didn't exist on the old type.
I see when I get the docs, I can filter down to all the
inside the
that I want to change, but then how do I create patches that will change the
of each of these blocks? It seems like maybe patches can only be performed on top-level documents and not child blocks - what's the best way to go about this?

doc.body.filter(block => block._type === 'ctaButton').map(block => ({
  id: block._key,
  patch: {
    set: {}
Apr 26, 2020, 4:31 AM
I believe you can use this script:
Edit the query:

client.fetch(`*['ctaButton' in body[]._type][0...100] {_id, _rev, name}`)
and edit the patch

    id: doc._id,
    patch: {
      set: {
        'body[_type=="ctaButton"]._type': "newCtaButton"
      // this will cause the migration to fail if any of the documents has been
      // modified since it was fetched.
      ifRevisionID: doc._rev

Apr 26, 2020, 5:29 PM
For future reference, this library might be interesting
Apr 26, 2020, 5:30 PM
Interesting! Will try that
Apr 26, 2020, 5:45 PM – build remarkable experiences at scale

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