🗓️ Everything *[NYC] is back. A free gathering for AI builders. Sept 9 →

How to query for document ID in Sanity GROQ?

8 repliesLast updated: Nov 29, 2025

What would possibly be a groq query for this
"result":[1 item
0:{10 items
"_createdAt":"2020-08-17T23:14:12Z"
"_id":"Gk1LBo8Kg5xEVuqFLjoonz"
"_rev":"Gk1LBo8Kg5xEVuqFLjoojw"
"_type":"products"
"_updatedAt":"2020-08-17T23:14:12Z"
"image":{2 items
"_type":"image"
"asset":{...}2 items
}
"name":"Testing Product"
"price":"5000"
"price_id":"price_1HHHWnHk5l44uIELkHjFxVc2"
"product_id":"prod_HqzVaPGvVvUR5s"
}
]
Where I get the document id as a result
AI Update

Based on your data structure, you're looking to query products and get back the document with all its fields including the _id. Here's a GROQ query that would return that result:

*[_type == "products"]{
  _id,
  _createdAt,
  _rev,
  _type,
  _updatedAt,
  name,
  price,
  price_id,
  product_id,
  image
}

If you want to query for a specific product (like the one in your example), you could filter by ID:

*[_type == "products" && _id == "Gk1LBo8Kg5xEVuqFLjoonz"][0]{
  _id,
  _createdAt,
  _rev,
  _type,
  _updatedAt,
  name,
  price,
  price_id,
  product_id,
  image
}

Or if you just need the document ID as the primary result, you could simplify to:

*[_type == "products"]{
  _id
}

A few key points about this query:

If you want to expand the image asset reference to get the full image data, you could use:

*[_type == "products"]{
  _id,
  name,
  price,
  price_id,
  product_id,
  image{
    _type,
    asset->
  }
}

The -> operator follows the reference to get the full asset document details.

Show original thread
8 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