Guidance on passing an ID to a subquery in a Slack thread.

3 replies
Last updated: Sep 7, 2023
Hello everyone,I have a requirement where I need to fetch all documents of the type hotel, and dynamically I have to pass the id to a subquery to fetch documents of the type restaurant that match the _id of hotel with ref _id.
Here is the query:

*[_type == "hotel"]
  hotelId, hotelName,
  *[_type == "restaurants" && participatingHotels[]._ref match *[_type == "hotel" && identifier == _id]._id] {
  "restaurantDesc": thumbnailDescription,
I tried this query, but it still had no use. Can someone guide me how to pass the _id to the subquery?
Sep 7, 2023, 11:29 AM
Does this work?
*[_type == "restaurants" && ^._id in participatingHotels[]._ref] {
Sep 7, 2023, 12:04 PM
refers to the enclosing document
Sep 7, 2023, 12:04 PM
user P
, Thanks, buddy. It's Working
Sep 7, 2023, 1:14 PM

Sanity– build remarkable experiences at scale

The Sanity Composable Content Cloud is the 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?