Build with Sanity MCP in Replit. Prizes up for grabs! Submit by June 14

How to dereference a field in GROQ while keeping original structure?

7 repliesLast updated: Nov 29, 2025

When using GROQ, how can I get the title of the document I'm referencing while keep all the other data I'm requesting?I have a list of images that have a few key:value pairs that I'll need, then there is one called
tags
that has a reference to
collections
each image can be tagged with many collections. I want to keep the structure I have and just convert the reference to the actual collection title
I'm sure this is easier than I'm making it, but I can't figure out how to structure my query. I'll post current query along with a screenshot of the data I'm request in the comments
AI Update

You can use the dereferencing operator (->) in GROQ to get the title of a referenced document while keeping all your other data. For your case with images that have tags referencing collections, you'll want to dereference the references within the array.

Here's how to structure your query:

*[_type == 'image']{
  ...,
  tags[]->{
    title
  }
}

Let me break down what's happening:

This will give you something like:

[
  {
    _id: "image-1",
    url: "...",
    // all your other image fields
    tags: [
      { title: "Collection A" },
      { title: "Collection B" }
    ]
  }
]

If you want just the collection titles as a flat array of strings instead of objects, you can use a naked projection:

*[_type == 'image']{
  ...,
  tags[]->title
}

This would give you:

{
  tags: ["Collection A", "Collection B"]
}

The key points:

You can read more about this in the GROQ documentation on expanding references.

Show original thread
7 replies

Was this answer helpful?

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.

Related contributions