How to group documents in Sanity - use Parent/Child relationship?

9 replies
Last updated: Dec 2, 2022
Is there a way to group documents? I have something for categories where I followed the Parent/Child relationship guide . I guess I just follow that anytime I want something to be grouped?
AI Update

There are several ways to group documents in Sanity, and the approach you choose depends on your specific use case:

Parent/Child Relationships with References

Yes, the parent/child relationship pattern you're already using for categories is a solid approach for grouping! This involves using reference fields to create hierarchical structures. You can implement this either:

  • Bottom-up: Child documents reference their parent (e.g., a blog post references its category)
  • Top-up: Parent documents reference their children (e.g., a category references all its posts)

The key thing to understand is that references are bidirectional when querying with GROQ, but they only appear in the Studio UI where you define the reference field. So if you want editors to manage the relationship when editing categories, put the reference field on the category document. If it makes more sense to assign categories when editing posts, put the reference on the post.

Other Grouping Approaches

Depending on your needs, you might also consider:

  1. Simple string fields: For basic categorization, you could use a string field with a predefined list of options. This works well when documents only belong to one group and the groups are relatively static.

  2. Array of references: When documents can belong to multiple groups, use an array of references instead of a single reference. For example, a blog post could reference multiple category documents.

  3. Shared field values: Sometimes you can query and group documents by any common field value (like a category string field) without needing references at all.

  4. Tags/taxonomy documents: Create separate document types for tags or categories that other documents can reference, giving you more flexibility to add metadata to your groupings.

The parent/child reference pattern is definitely a versatile solution you can reuse for different grouping scenarios. The hierarchies and graphs guide has more details on modeling these relationships, and remember you can use GROQ's -> operator to traverse references in either direction when querying your content.

Show original thread
9 replies
Are you looking to group documents within a single list? That would be possible, but you'd need to do some customization to the default list to get it to work.
However, if you're ok with having them under separate list items and in separate lists, you'll want to use
this method.
I basically want to see “Schools” in the list like it is and then when I click on it I would see Division A and Division B. Then clicking Division A I would see all the schools that fall under Division A and the same for Division B
But these schools could switch divisions so I don’t want to hard code it
Got it! You'd want to use the method I shared then. How are divisions shown in your schools?
Well those divisions are sacristy the two parent categories you see in the categories screenshot 😅
You'd want to do something like this (if the division is a document that's referenced by a school):
S.listItem()
    .title('Schools')
    .child(
      S.documentTypeList('division')
        .title('Schools by Division')
        .child(divisionId =>
          S.documentList()
            .title('Schools')
            .filter('_type == "school" && $divisionId == division._ref')
            .params({ divisionId })
        )
    ),
Finally able to get to this 😅 A couple of questions...1. How can I remove FCS and FBS from this?
2. The category "Playoffs" I have no idea where that came from... Any ideas how to figure that out
Ooo figured out #1
// Schools
      S.listItem()
        .title("Schools")
        .icon(FaSchool)
        .child(
          S.documentTypeList("category")
            .filter('_type == "category" && title != "FBS" && title != "FCS"')
            .title("Schools by Conference")
            .child((categoryId) =>
              S.documentList()
                .title("Schools")
                .filter('_type == "school" && $categoryId == conference._ref')
                .params({ categoryId })
Now I just have to figure out where "Playoffs" is coming from
Figured it out

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?