Go Behind the Experience to see how Tecovas brings the West to life with Sanity 🤠 July 18th

How to create a new document inside an array in Sanity

26 replies
Last updated: Sep 4, 2020
Hi everyone! What's the correct way to make a mutation for this type of data in my document?
{
  title: 'Contact options',
  name: 'options',
  type: 'array',
  of: [{ title: 'Option', type: 'option' }]
}

Sep 3, 2020, 8:29 PM
This works for editing existing ones under an ID, but how do I create a new document under the same ID?

patch: {

'id': id,

"set": {

'options': [{

'_type': 'option',

'_key': key,

'title': title,

'duration': duration,

'price': price

}]

}

}
Sep 3, 2020, 8:30 PM
Thank you. Due to the project setup I'm not using the client at the moment, although it does make it a lot easier I think
Sep 3, 2020, 8:35 PM
Is there a way to achieve this without?
Sep 3, 2020, 8:36 PM
Sep 3, 2020, 8:40 PM
Yes, with the exception that I'd like to create a new document and it seems patches are for modifying?
Sep 3, 2020, 9:39 PM
I have a document and inside this reference to another one:
{
  title: 'Contact options',
  name: 'options',
  type: 'array',
  of: [{ title: 'Option', type: 'option' }]
}

Sep 3, 2020, 9:50 PM
I want to create a new item (type option) inside this document (profile). I am able to edit existing option items with the below patch query, where ID is the name of parent document (profile):
patch: {

'id': id,

"set": {

'options': [{

'_type': 'option',

'_key': key,

'title': title,

'duration': duration,

'price': price

}]

}

}
Sep 3, 2020, 9:52 PM
Sep 3, 2020, 9:53 PM
Exactly, but the same query doesn't work with "create" instead of "patch"
Sep 3, 2020, 9:53 PM
Can't figure out how to write the correct query?
Sep 3, 2020, 9:53 PM
"mutation failed on document "...": Document does not have a type"
Sep 3, 2020, 9:54 PM
create: {

'id': id,

"set": {

'options': [{

'_type': 'option',

'_key': key,

'title': title,

'duration': duration,

'price': price

}]

}
}
Sep 3, 2020, 9:56 PM
also tried without "set"
Sep 3, 2020, 9:56 PM
You need to provide a document type

_type: 'your-document-type'
Sep 3, 2020, 9:58 PM
yes - I'm sending '_type': 'option'
Sep 3, 2020, 9:58 PM
same as patch query
Sep 3, 2020, 9:58 PM
On the root document
Sep 3, 2020, 9:58 PM
create: {

"_id": id,
 "_type": "your-type",
  "options": [...]

}
Sep 3, 2020, 10:01 PM
(on mobile so excuse my brevity)
Sep 3, 2020, 10:01 PM
Ok, got it! That doesn't result in error but also doesn't create the option item 🤔
Sep 3, 2020, 10:01 PM
strange - still works with "patch" but just overrides the item every time. but doesn't do anything with "create" + document type
Sep 3, 2020, 10:06 PM
the goal is to actually modify an existing document by creating a new one inside
Sep 3, 2020, 10:13 PM
wondering if it has to be patch + create somehow?
Sep 3, 2020, 10:13 PM
patch: {

'id': id,

"insert": {

"before": "options[-1]",

'items': [{

'_type': 'option',

'_key': key,

'title': title,

'duration': duration,

'price': price

}]

}

}
Sep 4, 2020, 9:37 AM
this is what worked
Sep 4, 2020, 9:37 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?