How to group a document by a specific field in Sanity.io

6 replies
Last updated: Oct 22, 2021
Hello guys, I need a little help about how I can group a document by a specific field. For example, I have
physician
document where I have
category
string filed which can be leadership, radiologist etc. I would like a result where I can have
{
"leadership": [],
"radiologist": []
}
Oct 22, 2021, 12:00 AM
Hey User! Just to clarify, are you trying to get the 'leadership' list to show all doctors whose category is leadership? What kind of schema is
category
?
Oct 22, 2021, 12:15 AM
So, I have a
physician
document where I have sample data like the following
[
  {
    "category": "radiologist",
    "name": "J. Michael Barraza, Jr., MD"
  },
  {
    "category": "leadership",
    "name": "User, M.D."
  },
  {
    "category": "leadership",
    "name": "User, M.D"
  }
]
I like to have a result that will be grouped by its category. So I would like to have an output that should be like

{ radiologist: [ 
{ category: 'radiologist', name: 'J. Michael Barraza, Jr., MD' } 
  ],
  leadership: [ 
{ category: 'leadership', name: 'User, M.D.' },
{ category: 'leadership', name: 'User, M.D' } 
    ] 
}
Oct 22, 2021, 2:09 AM
you can do a subquery like this:
client.fetch(`{
  "radiologist": *[category == "radiologist"],
  "leadership": *[category == "leadership"],
}`)
Oct 22, 2021, 3:55 AM
but what if I don’t know what user will give me input. As I said these are just string. I just would like this to be dynmic.
Oct 22, 2021, 3:56 AM
Ah, so this is for a search query, i.e searching for a person named Scott? If you have each category as a reference instead of a string, it’d be easier. Say your data looks like this instead

[
  "_type": "physician",
  "category": {
    "_type": "reference",
    "_ref": "abc123"
  },
  "name": "J. Michael Barraza, Jr., MD",
},
{
  "_type": "category",
  "_id": "abc123",
  "name": "radiologist"
}
]
Then you can query based on category

*[_type == 'category'] {
  "category": name,
  "results": *[
    _type == "physician"
    && name match "Mic*"
    && references(^._id)
  ]
}
The result will look like this


[{
  "category": "radiologist",
  "results": [
    {
      name: "J. Michael Barraza, Jr., MD",
    }
  ]
}, {
  /* other categories */
}]
AFAIK you can’t project an object with dynamic keys, so this is the closest to your ideal results — I might be wrong though.

Without a category, you can still query, but it requires listing all categories in advance, like the query I shared above.

This is a working example in groq.dev:

https://groq.dev/3GA9zOXffsuJcE9IfuB7wi
Oct 22, 2021, 5:09 AM
Yeah that’s what I thought at first to create a category document type. But I was wondering if I can group with just string field. Thank you
Oct 22, 2021, 6:44 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?