Join our next Developer Virtual Meetup – Live Q&A, Project Showcase, Swag raffle, and more 🔥

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]
Jan 6, 2023, 9:25 PM
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.
Jan 7, 2023, 11:27 PM
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.
Jan 7, 2023, 11:30 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?