Get _key field when dereferencing array of references in GROQ

13 replies
Last updated: Nov 30, 2025
Hi all! When i'm querying an array of references, is there any way to get BOTH the resolved reference and the _key field?When I do this:

products[]
I get the unresolved references with their _key, _ref fields etc.
When I do:
products[]->
, I get the resolved references, without the _key field.I need the _key to have a unique identifier of the array entry in my application.
AI Update

Great question! Yes, you can absolutely get both the resolved reference data and the _key field. The trick is to use GROQ's object projection syntax to explicitly select the fields you want.

Instead of just products[]->, you need to construct a new object that includes both the _key from the reference object and the dereferenced document:

products[] {
  _key,
  "item": @->
}

Or if you want to spread all the fields from the referenced document directly alongside _key:

products[] {
  _key,
  ...@->
}

The @ symbol refers to the current element in the array, and @-> dereferences it. The spread operator ... pulls all fields from the dereferenced document into your result object.

If you only need specific fields from the referenced document, you can be more selective:

products[] {
  _key,
  "productName": @->name,
  "productPrice": @->price,
  "productSlug": @->slug.current
}

This pattern is covered in the GROQ Query Cheat Sheet and there's also a helpful community answer thread about keeping the _key property when resolving references.

The key insight is that when you use products[]->, you're replacing each reference object with the dereferenced document, which is why you lose the _key. By using object projection with {}, you maintain control over exactly what fields end up in your result.

Show original thread
13 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?