Retrieving Documents not in an Array

2 replies
Last updated: Jan 7, 2023
I have an array of ids. I want to retrieve all documents that don't contain those ids. For example, these are all the document ids : [1,3,4,5]. I have an array of [3,4]. Is there a way to retrieve documents not in the array of [3,4]? In this case, it returns [1,5]
You will need to get all the id's and then reduce them down to a single array on the client side. If not you could generate a query using the
select
operator in the query.

*[type == document]{

'filteredIds': select(

_id != ${id1},
 _id != ${id2}

)

}
Where
document
is the name of the document you are searching. Here
filteredIds
would be the key in the object returned.
The second one ony works if you know how many items will be in the [3,4] array and set each value to a variable first.

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?