Unlock seamless workflows and faster delivery with our latest releases - Join the deep dive

Discussion on how to use referenced document ID in filter for GROQ queries.

6 replies
Last updated: May 24, 2021
Hey guys. I’ve see a lot of GROQ related questions here recently so apologies if I’m duplicating myself but… is there a way to get a referenced document id to be later used in filter?
I have a nested array for example with different types of objects with references, but when I try to use
references
filter inside it as such I get nothing:

_type == 'contentSubjectTag' => {
  _type,
  "title": subjectTag->title,
  "text": subjectTag->text,
  "subjects": *[_type=='subject' && references(subjectTag->_id)]{
    title,
    externalLink,
  },
},
May 24, 2021, 6:16 PM
Hi
user H
not sure where the scope of
subjectTag
is being returned but you can try this.

_type == 'contentSubjectTag' => {
  _type,
  "title": subjectTag->title,
  "text": subjectTag->text,
  "subjects": *[_type=='subject' && references(subjectTag[]._id)]{
    title,
    externalLink,
  },
},
However you can reference the
subject
type by passing in it's container/parent
_id
like so.

_type == 'contentSubjectTag' => {
  _type,
  "title": subjectTag->title,
  "text": subjectTag->text,
  "subjects": *[_type=='subject' && references(^._id)]{
    title,
    externalLink,
  },
},
Let me know if these help.
May 24, 2021, 6:24 PM
Hey
user B
, thanks for getting back to me!

subjectTag
is a reference within
contentSubjectTag
so I need to use its ID really. I’ve tried the examples you’ve listed but unfortunately they didn’t work : (
Here’s a fuller example:

*[_type == "home"][0]{
  contentColumns[]{
    title,
    contentBlocks[]{
      _type == 'contentSubjectTag' => {
      	_type,
				"_id": subjectTag->_id,
				"title": subjectTag->title,
        "text": subjectTag->text,
        "subjects": *[_type=='subject' && references(subjectTag[]._id)]{
          title,
          externalLink,
        },
      },
    }
  },
}
May 24, 2021, 6:33 PM
Maybe you can try this.

*[_type == "home"][0]{
  contentColumns[]{
    title,
    contentBlocks[]{
      _type == 'contentSubjectTag' => {
      	_type,
		"subjectId": subjectTag->_id,
				"title": subjectTag->title,
        "text": subjectTag->text,
        "subjects": *[_type=='subject' && references(subjectTag[]._id)]{
          title,
          externalLink,
        },
      },
    }
  },
}
May 24, 2021, 6:37 PM
Alright, think I’ve got it with the following:
_type == 'contentSubjectTag' => {
  _type,
  _id,
  "_id": subjectTag->_id,
  "title": subjectTag->title,
  "text": subjectTag->text,
  "subjectTag": subjectTag,
  "subjects": *[_type=='subject' && references(^.subjectTag._ref)]{
    title,
    externalLink,
  },
},
May 24, 2021, 6:41 PM
Maybe you can try this.

*[_type == "home"][0]{
  contentColumns[]{
    title,
    contentBlocks[]{
      _type == 'contentSubjectTag' => {
      	_type,
// Best not to use '_' in your projection as it is saved for reserved properties
		"subjectTagId": subjectTag->_id,
		"subjectTitle": subjectTag->title,
        "subjectText": subjectTag->text,
          "subjects": *[_type=='subject' && _id == ^.subjectTagId ]{
// OR
//        "subjects": *[_type=='subject' && references(^.subjectTagId)]{
          title,
          externalLink,
        },
      },
    }
  },
}
Might not be understanding what you are trying to achieve but here is what I was thinking. Since you already have parent scope reference to the
subjectTagId
you can maybe use that to get access to the data you need.
May 24, 2021, 6:45 PM
Maybe you can try this.

*[_type == "home"][0]{
  contentColumns[]{
    title,
    contentBlocks[]{
      _type == 'contentSubjectTag' => {
      	_type,
// Best not to use '_' in your projection as it is saved for reserved properties
		"subjectTagId": subjectTag->_id,
		"subjectTitle": subjectTag->title,
        "subjectText": subjectTag->text,
          "subjects": *[_type=='subject' && _id == ^.subjectTagId ]{
// OR
//        "subjects": *[_type=='subject' && references(^.subjectTagId)]{
          title,
          externalLink,
        },
      },
    }
  },
}
Might not be understanding what you are trying to achieve but here is what I was thinking. Since you already have parent scope reference to the
subjectTagId
you can maybe use that to get access to the data you need.
May 24, 2021, 6:45 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?