Explaining self-referencing category array without category name field

13 replies
Last updated: Aug 8, 2020
could you explain your schema a little bit it seems like categories is self referencing array but there is no value like Category name etc...?
AI Update

It sounds like you're looking at a schema where there's a categories field defined as an array, but it's not clear what's actually being stored in it. Let me help clarify what might be happening!

Based on your description, it seems like you have a self-referencing array, which typically means an array of references that point back to the same document type. However, you're right to be confused if there's no clear "Category name" or other identifying fields visible.

Common Patterns & What Might Be Going On

Self-Referencing Array Pattern

A typical self-referencing categories array might look like this in your schema:

{
  name: 'categories',
  type: 'array',
  of: [
    {
      type: 'reference',
      to: [{type: 'category'}]
    }
  ]
}

This would reference separate category documents. If you're not seeing category names, you might need to check:

1. The referenced document type - Look for a separate category schema definition that should have fields like:

{
  name: 'category',
  type: 'document',
  fields: [
    {
      name: 'title',
      type: 'string',
      title: 'Category Name'
    }
    // ... other fields
  ]
}

2. The preview configuration - The preview property determines what you see in the Studio. If it's missing or misconfigured, you might not see the category names displayed properly. You can add a preview configuration to make the category names visible:

preview: {
  select: {
    title: 'title'
  }
}

Alternative: Inline Objects

If it's truly self-contained (not references), it might be an array of objects:

{
  name: 'categories',
  type: 'array',
  of: [
    {
      type: 'object',
      fields: [
        {
          name: 'name',
          type: 'string',
          title: 'Category Name'
        }
      ]
    }
  ]
}

Troubleshooting Steps

Without seeing your actual schema code, here's what to check:

  1. Look for a separate category document type in your schema files
  2. Check if the preview configuration exists on that category type
  3. Verify the of property in your categories array has the correct type reference
  4. Make sure the referenced type has actual content fields like title, name, etc.

As mentioned in the array fields documentation, arrays need a valid of property that defines what types they contain, and each member type should have appropriate fields to display meaningful content. The array itself is just a container - the actual data structure comes from what you define in the of array.

If you can share the schema definition for both the categories field and any related category document type, I can give you more specific guidance on what might be missing!

Show original thread
13 replies
Thank you for your message! My Categories schema looks like this

export default {
  name: "categories",
  title: "Categories",
  type: "document",
  fields: [
    {
      name: "title",
      title: "Title",
      type: "string",
    },
    {
      name: "slug",
      title: "Slug",
      type: "slug",
      options: {
        source: "title",
        maxLength: 96,
      },
    },
    {
      name: "description",
      title: "Description",
      type: "text",
    },
    {
      name: "featuredImage",
      title: "Featured image",
      type: "image",
      options: {
        hotspot: true,
      },
    },
  ],
};
There is no name, but there is a
title
and
slug
field. So I'm trying to understand how I can make groq query to look into those fields. maybe something like in this query https://sanity-io-land.slack.com/archives/C011CAT70DD/p1586980014009300?thread_ts=1586979557.003700&cid=C011CAT70DD but this didn't work for me when I did

*[ _type== 'post' && categories->title == 'category name']
also tried this
*[_type == 'posts' && categories.title match 'category name' ]
I think I'm not getting the syntax
Because Categories would be an array I think you need categories [].title *[_type == 'posts' && 'category name' in categories []. title]
yeah, it does make sense. however, I still get
400
error. I'm making requests with axios. simple
*[_type=='posts']
works, get all the posts, but when I try to be more specific, I get 400. can it be that axios tampers with request somehow, do you know?
If you have vision deployed on your studio probably a good idea to test query in vision then investigate further, but No I have never used axios for sanity calls.
okay, I'll go try with vision, thank you for your help!
*[_type == 'posts' && categories[]->title match 'category name' ]


Try this one? You have to traverse the array of references
categories[]
and then join the category document
->
and pick the
title
field
YASS! it worked, thank you. Also this worked. What's the difference between yours and this one?
*[ _type=='posts' && 'category name' in categories[]->title]
also follow up question, how do you include multiple categories names? do I have to
&& 'category name' in categories[]->title
every time or is there a shorter version?
Match can be fuzzy if you e.g add a
*
wildcard to the string. The
in
is only true on exact match
ah, makes sense, thank you! I'm gonna write a blog post to help others out as well! 👍
Not 100% sure about array comparisons tbh. And on my iPhone, so a bit hard to experiment:)
No worries, then I'll play around with it Was just asking in case there is a specific 'documented' way of doing things. Thanks a lot!

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?