How to structure a schema in Sanity.io to allow for multiple documents

16 replies
Last updated: Jun 27, 2022
hi ,
Is it possible to be able to add more then 1 document ? how do I structure this in the schema?
AI Update

Looking at your question, I believe you're asking about how to reference multiple documents in a schema field. The most common way to do this in Sanity is using an array of references.

Here's how you structure it in your schema:

{
  name: 'relatedArticles',
  title: 'Related Articles',
  type: 'array',
  of: [
    {
      type: 'reference',
      to: [{type: 'article'}]
    }
  ]
}

This allows editors to add multiple document references to a single field. If you want to reference multiple different document types in the same array, you can do:

{
  name: 'resources',
  title: 'Resources',
  type: 'array',
  of: [
    {
      type: 'reference',
      to: [
        {type: 'article'},
        {type: 'video'},
        {type: 'podcast'}
      ]
    }
  ]
}

As explained in the array fields documentation, array fields create orderable collections where editors can:

  • Add as many items as needed
  • Reorder them via drag-and-drop
  • Remove items
  • Each item gets a unique _key automatically

You can also mix different types within the same array if you need more complex structures:

{
  name: 'content',
  title: 'Content Blocks',
  type: 'array',
  of: [
    {
      type: 'reference',
      to: [{type: 'article'}]
    },
    {
      type: 'image'
    },
    {
      type: 'object',
      name: 'textBlock',
      fields: [
        {name: 'text', type: 'text'}
      ]
    }
  ]
}

The array approach is much more flexible than single reference fields and is the standard Sanity pattern for "one-to-many" relationships. Editors can add unlimited documents (though you can add validation rules to set min/max limits if needed).

You would need to have a field that’s an array of files/images.
{
  type: "array",
  of: [ { type: "image" } ]
}
my documents schema looks like this. where should I fit the array in ?
{
        title: 'Document',
        description: "Place document here",
        name: 'file',
        type: 'file',
        fields: [
          {
            name: 'description',
            type: 'string',
            title: 'Description'
          }
        ]
      },
try something like
{
  title: 'Documents',
  description: 'A list of documents',
  name: 'documents',
  type: "array",
  of: [
    {
      title: 'Document',
      description: "Place document here",
      name: 'file',
      type: 'file',
      fields: [
        {
          name: 'description',
          type: 'string',
          title: 'Description'
        }
      ]
    }
  ]
}
but personally, i would probably turn the single Document into its own type, then reference that perhaps... really depends on your need/use case
Yea I was thinking about that to just reference the docs and if needed create
it also gives you the advantage to re-use that document elsewhere if you needed, rather than it being tied to a single place
yes thats the better way to do it
how do I transform the reference so that I can add more then 1 ?
you would need to make it a
document type
like
type: 'document'
instead of
type: 'file
, then move the file type down into the fields
then you could make an array of reference to that new type in your original code
hmm could you show an code example of how to structure this?
sure, here's something similar i have for doing "categories" on a blog
first i create the
category
document type
export default {
  title: "Category",
  name: "category",
  type: "document",
  fields: [
    {
      title: "Title",
      name: "title",
      type: "string",
    },
    {
      title: "Slug",
      name: "slug",
      type: "slug",
      options: {
        source: 'title',
        maxLength: 200, // will be ignored if slugify is set
        slugify: input => input.toLowerCase().replace(/\s+/g, '-').slice(0, 200)
      },
      validation: Rule => Rule.required()
    },
    {
      title: "Description",
      name: "description",
      type: "text"
    }
  ]
}
then i add the reference array to my
article
type
export default {
  title: "Article",
  name: "article",
  type: "document",
  fields: [
    {
      title: "Title",
      name: "title",
      type: "string"
    },
    {
      title: "Categories",
      name: "categories",
      fieldset: 'details',
      type: "array",
      of: [
        {
          type: "reference",
          to: [{type: "category"}]
        }
      ],
      options: {
        sortable: false
      },
      preview: {
        select: {
          title: "title"
        }
      }
    }
  ],
}
i tried to strip out all the unrelated code in this example
I see you have seperated the schemas, how does the
fieldset: 'details',
acctualy work?
oh, i missed stripping that out ... its hard to explain without the rest of the fieldset code
i would remove that if you're trying to use my code as a 1:1 proxy
This was exactly what I was looking for thanks
user G
!

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?