How to query for document ID in Sanity GROQ?

8 replies
Last updated: Aug 18, 2020
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:

  • The *[_type == "products"] part filters all documents to only those with type "products"
  • The curly braces {} define a projection that specifies which fields to return
  • System fields like _id, _createdAt, _rev, etc. need to be explicitly listed if you want them (they're not included by default in projections)
  • The [0] in the second example gets just the first matching document instead of an array

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
Assuming you’re having your key on
product_id

a groq query could look like this:

*[_type == "products" && product_id == "prod_HqzVaPGvVvUR5s]{
    _id
}

Assuming you’re having your key on
product_id

a groq query could look like this:

*[_type == "products" && product_id == "prod_HqzVaPGvVvUR5s]{
    _id
}

I do have the product id I tried something like this
      const query = `*[_type == "products" && product_id == ${payload.data.object.product}]`;
consoling the payload does give me the prodict_id now this is my fetch

 client.fetch(query).then((products) => {
        console.log("products");
        console.log(products);
        console.log("products");
      });
Does this work is params needed?
I just get a empty array in the fetch
when I console my query it looks like this
*[_type == "products" && product_id == prod_HqzVaPGvVvUR5s]
your product ID should probably be wrapped in quotes, though i’m not sure that’ll solve the issue
(also as an aside, I highly recommend installing the Vision plugin for testing your GROQ queries: https://www.sanity.io/docs/the-vision-plugin you can use it to test the explicity query you just logged and make sure that’s functioning as you expect it to)
Got it, it needed the quotes. Thanks!

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?