Geoff Ball
Support Engineer at Sanity.io
GROQ query that finds duplicate data in a given field.
// GROQ query
*[_type == $type] {
"duplicates": *[_type == $type && @[$findDups] == ^[$findDups] && _id != ^._id && !(_id in path('drafts.**'))] {_id, $findDups: @[$findDups]}
}[count(duplicates) > 0]
// Params
{
"type": "post",
"findDups": "title"
}
This GROQ query takes advantage of Equijoins to search all documents of a particular type and return the _id
and duplicated field so that action can be taken. One might use this query in Vision to see duplicates at a glance (or make modifications in the Studio), or from the Sanity CLI (or a program like Insomnia or Postman) where mutations could be made directly.
It is recommended to use a newer version of the GROQ API (e.g., v2021-03-25
) due to speed enhancements made since v1
. In the event that the query times out when used with a large dataset, you may want to add a slice to the end of the query (e.g., [0..50]
).
This query depends on two params
:
: The document type to search. Change
"type": "post"post
to your desired document type.
: The field to search for duplicates. Change
"findDups": "title"title
to the name (in the schema) of the field in which to search for duplicates.
The user can quickly change the document type (type
) and field to scan for duplicates (findDups
).
The query comes in three parts:
*[_type == $type]
: Filter for all documents that have a _type
of $type
(pulled in from params
).{ "duplicates": *[_type == $type && @[$findDups] == ^[$findDups] && _id != ^._id && !(_id in path('drafts.**'))] {_id, $findDups: @[$findDups]} }
: Run another query that returns documents of that same _type
that have duplicated data in the field named in findDups
but whose _id
s don't match. From that, return only _id
and the field named in params
as findDups
(for brevity) and assign it all to the key duplicates
.[count(duplicates) > 0]
: From all the data assigned to duplicates
, return only those that actually have duplicates. There's no need to see the rest.Support Engineer at Sanity.io
This snippet can be used to fetch current, previous and next articles based on publication date and related tags.
Go to Get current, previous and next post, filtered by tagsIf you'd like to be able to reference your project users within your content, you'll need to create a document for them.
Go to Create A Document for All Current Project UsersScript to batch remove a field from all documents that match a GROQ query
Go to Batch remove a fieldA custom input component that lets the schema creator control max, min, and step values on a number input.
Go to Controlled Number Custom Input Component