Compare two string arrays for common elements in GROQ

10 replies
Last updated: Jan 28, 2026
Hi everyone, is it possible to compare two arrays (of strings) and check if any strings are in both arrays with GROQ?for example...

['hello', 'world']
and
['sanity', 'cms']
would return false
['*hello*', 'world']
and
['*hello*', 'sanity']
would return true as
'hello'
is in both arrays
AI Update

Yes, absolutely! You can check if two arrays have any overlapping strings using the in operator combined with count() in GROQ. Here's how:

count(['hello', 'world'] @ in ['hello', 'sanity']) > 0

This would return true because 'hello' appears in both arrays.

How it works

The @ symbol in GROQ represents "each element" in the array. The expression array1 @ in array2 iterates through each element of array1 and checks if it exists in array2, returning an array of matching elements. Then count() tells you how many matches were found.

Breaking it down:

  • ['hello', 'world'] @ - iterates through each element ('hello', then 'world')
  • @ in ['hello', 'sanity'] - checks if the current element exists in the second array
  • count(...) - counts how many elements matched
  • > 0 - returns true if there's at least one match

Practical example with documents

This is especially useful when filtering documents. For example, if you have posts with tags and want to find posts that have any tags matching a specific list:

*[_type == 'post' && count(tags[] @ in $selectedTags) > 0]

Or checking if two reference arrays have any overlap:

*[_type == 'article' && count(authors[]._ref @ in $authorIds) > 0]

Checking for complete intersection

If you want to check that all elements from one array exist in another (not just any), you can compare the count to the array length:

count(['hello', 'world'] @ in ['hello', 'world', 'sanity']) == 2

The count() function works perfectly with the in operator for these kinds of array intersection checks. This pattern of count(array1 @ in array2) > 0 is a common and efficient way to check for array overlap in GROQ queries, and it's particularly powerful when used in filter components to narrow down your document results based on array intersections.

Show original thread
10 replies

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?