🔮 Sanity Create is here. Writing is reinvented. Try now, no developer setup

Filtering for unique data in a query and handling duplicate categories in Sanity.io.

7 replies
Last updated: Mar 31, 2021
Hi sanity team Just wanted to know whether is their a way to filter in a query which will result in all unique data.
Eg if we are retrieving title only and there is a title found twice , the filter query should show it only once
Is it possible or is it better we do it at the frontend
Mar 30, 2021, 8:28 AM
It may be possible but this sounds like something that could create some nasty bugs.
Whenever you filter by something, that something you filter by should be unique (unless you are say filtering all documents of a certain type). You can use the sanity isUnique-functionality to enforce uniqness in your slugs.

For things like posts, you can have as many posts with the same title as you want - as long as you treat them by something else in the front end (for example slug, which should always be unique)
Mar 30, 2021, 8:36 AM
My goal is filter out the categories of products (e-commerce), so i can have a groq queries which contains a unique list of categories.
I am filtering all document of a certain type.
Mar 30, 2021, 8:40 AM
Should i move ahead with the sanity function you mentioned
Mar 30, 2021, 8:41 AM
You have duplicate categories in your solution? I usually handle categories as it’s own document that I can pull from, I have not used isUnique for categories - mostly because I usually controll the sites I use and have never inserted duplicated categories.
I would try the funtion, the docs only mention it working for slug but I don’t see why it shouldn’t work for other things as well
Mar 30, 2021, 8:49 AM
What i meant was that there can be more than 1 item with the same category name code-example .I think i should create separate schema for category creation maybe, which will reference to the product schema where we can select the product categories from a dropdown.What do you think?
Mar 30, 2021, 10:15 AM
Hey
user C
just figured out the starter im using has already coded the logic which references the product item. I thought categories were made when we type a category in the product field instead of the dropdown which references the category schema.
Just wanted to tell it’s sorted now , thanks a lot for helping out
Mar 30, 2021, 5:02 PM
I believe they are working on a
destinct
method which will allow to return unique items as per your o.p
Mar 31, 2021, 12:21 PM

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?