How to get the title of a referenced document while keeping all other data in GROQ.

7 replies
Last updated: Jan 28, 2022
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:

  1. ... - The ellipsis operator keeps all existing fields from your image documents
  2. tags[] - The square brackets traverse the array of tags
  3. -> - The dereferencing operator follows each reference to get the actual collection document
  4. {title} - This projection extracts just the title from each collection

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:

  • Use [] to traverse arrays of references
  • Use -> to dereference (follow) the reference
  • Use {title} to project just the fields you want, or .title for a naked projection to get just the values

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

my query
*[_type == "quiz-v2" && setDefault == true][0] {
    design_inspo_builder[3]
	
	 }

what i'm getting back
Correct me if I'm misunderstanding your question, but is your ultimate goal to get all of the fields, as well as the one you're defining in your current query? If so, you can use
...
, like so:
*[_type == "quiz-v2" && setDefault == true][0] {
    ...,
    design_inspo_builder[3]
}
not exactly, I want everything in
design_inspo_builder[3]
, but once I get to the nested tags inside the image, I would also like the title of those tags.
So, I would still have the object as is, but tags would look like

tag:[
{
"_key":"14a797683a38"
"_ref":"7bce91b5-6465-473b-991a-d75ffa7aab7a"
"_type":"reference"
"tag_name": "name of the tag"
}
]
I see now!
*[_type == "quiz-v2" && setDefault == true][0] {
    design_inspo_builder[3] {
      ...,
      image_options {
        images[]{
          ...,
          tags[]->
        }
      }
  }
}
not exactly, I want everything in
design_inspo_builder[3]
, but once I get to the nested tags inside the image, I would also like the title of those tags.
So, I would still have the object as is, but tags would look like

tag:[
{
"_key":"14a797683a38"
"_ref":"7bce91b5-6465-473b-991a-d75ffa7aab7a"
"_type":"reference"
"tag_name": "name of the tag"
}
]
Amazing!!!! Thanks so much!
I was doing something similar, but had left off the array brackets for images and tags and was getting no results
🤦

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?