How to get the number of nested/children components in a GROQ query.

3 replies
Last updated: Jan 19, 2024
hello all šŸ‘‹ I have the following GROQ: I’m retrieving a list of components as ā€˜items’. Now I need the number of nested/children components each parent component has. How can I get the number of each component’s children/subpages? I was trying to obtain this number inside ā€˜children’.

{
            template,
            layout_library {
                hero_title,
                components_section_title,
                'items': *[_type == 'components' && layout_components.is_parent == true] | order(title asc){
                    title,
                    _id,
                    slug,
                    layout_components {
                      component_image${imageQuery},
                    }
                },
                'children': *[_type == 'components' && layout_components.is_parent == false] | order(title asc){
                    ???
                },
            },
        }
AI Update

Looking at your GROQ query, you want to count how many child components each parent component has. Based on the count() function documentation, you can use count() with a filter to count references.

Here's how to modify your query to add a count of children to each parent component:

{
  template,
  layout_library {
    hero_title,
    components_section_title,
    'items': *[_type == 'components' && layout_components.is_parent == true] | order(title asc){
      title,
      _id,
      slug,
      layout_components {
        component_image${imageQuery},
      },
      'childrenCount': count(*[_type == 'components' && layout_components.is_parent == false && references(^._id)])
    },
    'children': *[_type == 'components' && layout_components.is_parent == false] | order(title asc){
      title,
      _id,
      // other fields you need
    },
  },
}

The key part is 'childrenCount': count(*[_type == 'components' && layout_components.is_parent == false && references(^._id)]). This:

  1. Uses count() to count documents
  2. Filters for child components (layout_components.is_parent == false)
  3. Uses references(^._id) to find components that reference the current parent (the ^ accesses the parent scope's _id)

This assumes your child components have a reference field pointing to their parent. If your relationship structure is different (like if parents have an array of child references), you'd adjust accordingly:

'childrenCount': count(childReferencesArray)

If you need to count children based on a different relationship structure, let me know how your parent-child relationship is defined in your schema and I can adjust the solution!

'children': *[count(_type == 'components' && layout_components.is_parent == false)]
thanks
user E
! but this wouldn’t work as I don’t need just one number.
Let me explain this a little bit more. I’ve got a Library page for a Design system where I’m looping main components like Button and Accordion. Button has 3 types of buttons/subpages: button/button-01, button/button-02 and button/button-03.

In Library, I’m looping the components that are parent pages and show the info of them. In this case the title for Button and a description. And then, just under the the description it says ā€œ3 button types availableā€. how do I get the 3 for button, the X for accordion and other numbers for other components?
āœ… fixed it this way:
'items': *[_type == 'components' && layout_components.is_parent == true] | order(title asc){
                    title,
                    _id,
                    slug,
                    'total': count(*[_type == 'components' && parent_component == ^.slug.current]),
                    
                },

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?