Questions tagged [groq]

GROQ - Graph Oriented Query Language, is a general purpose query language and can query any collection of unstructured JSON-documents without any up-front configuration.

GROQ - Graph Oriented Query Language

GROQ is developed by SANITY.io as is used to query data either hosted with SANITY or self-hosted. You usually write the queries in Content Studio and display the data in your front-end (vue.js, react, angular etc.).

Join together information from several sets of documents. Stitch together a very specific response the exact fields you need.

  • Follow references
  • Relational joins on key-value data
  • Get exactly the data structures you need by reprojecting attributes
  • Bundle multiple queries in the same request and get it all cached.
  • Query on structured block text

Everything:

*

Movies released after 1979

*[_type == 'movie' && releaseYear > 1979]

… ordered by release year and only certain fields

*[_type == 'movie' && releaseYear >= 1979]
  | order(releaseYear) {
    _id, title, releaseYear
  }

Get actors and join in arrays of the titles of the movies they played in

*[_type == "person"]{
  _id, name,
  "movies":
    *[
      _type == "movie"
      && references(^._id)
    ].title
}
78 questions
0
votes
0 answers

How to filter on field inside array of references using Sanity GROQ?

Given this result from Sanity vision, how can I correctly filter out the shows array to only return shows that are active === true? { "result": { "_createdAt": "", "_id": "xyz", "_rev": "abc", "_type": "content", "active":…
Karl Taylor
  • 4,839
  • 3
  • 34
  • 62
0
votes
0 answers

Display an Array Data Referencing a Category Array with Sanity and Next.js

I'm trying to have a Category array display on the frontend with items that reference the Category below it. I've been able to render the category titles on the page, but not the items with references to that category by doing: export default…
0xnikr
  • 1
  • 3
0
votes
0 answers

Is it possible to assign a `rank` from another field using a groq query

I only have a score and entrant field, but want to assign a rank based on the score value. Where scores are equal, the rank is equal, but then would skip over the next n ranks to "realign" the…
Designer023
  • 1,902
  • 1
  • 26
  • 43
0
votes
0 answers

Sanity GROQ: Order by geo::distance

I would like to order results in a GROQ by distance. Something like: "places": *[ _type == 'place' ] { name } | order(geo::distance(^.start->location, location) desc), but this doesn't seem to have the intended result. Is it…
Brett DeWoody
  • 59,771
  • 29
  • 135
  • 184
0
votes
2 answers

GROQ: query every element in an array

I'm using Sanity/nextjs, and I want to query/fetch every image and their details out of an array (this is a unique slug). I've looked at the cheat sheet, and I've tried numerous methods of trying to do it, but nothing ever shows up on the page and…
paparonnie
  • 170
  • 8
0
votes
0 answers

How to query for posts in a particular category in sanity

I have a card that's supposed open a backdrop with a carousel of some videos. I'm getting all the categories and videos from sanity.io Here's the card {categories && categories.map((category)=>{ return(
0
votes
1 answer

returned null values in getStaticPaths

I am trying to return in my params the values of the slug and tags , please notice that tags is not an array it's just a string. export async function getStaticPaths() { const chapters = await client.fetch( `*[_type == "chapters" &&…
0
votes
1 answer

query by reference sanity.io

I have three schemas docTopics => subTopics => article that are references of each other. I wanted to write a query for docTopics to return all the subTopics references to docTopics export const queryRefrence = groq` { "topics": *[_type ==…
sep
  • 29
  • 4
0
votes
2 answers

how to filter only one data with a specific slug in sanity.io?

Data: [ { "name": "Gates of Olympus", "slug": { "_type": "slug", "current": "gates-of-olympus" } }, { "name": "Floating Dragon", "slug": { "_type": "slug", "current": "floating-dragon" } }, …
Sai Krishnadas
  • 2,863
  • 9
  • 36
  • 69
0
votes
1 answer

Sanity.io GROQ - Retrive document by highest reference count in other documents

I have a tag document that looks like { name: 'tag', title: 'Tag', type: 'document', fields: [ { name: 'label', title: 'Label', type: 'string', } ] } And page document that looks like { name: "page", title:…
0
votes
1 answer

Sanity GROQ: order results by the value of a referenced field

I have the document type celeb. And the document type fact. Each fact references a celeb. I want to list celebs ordered by their most recently updated facts. So the celeb which has just had a fact added about them would show up at the top. How can I…
M.K. Safi
  • 6,560
  • 10
  • 40
  • 58
0
votes
1 answer

Check in groq query if array of objects contains a case insensitive string

I have an object that has an array called tags. It is array of objects. I need to query those objects that its tags contains a case insensitive string. [ { "_createdAt": "2022-02-18T09:16:27Z", "_id": "article-13000018493", "_rev":…
mahan
  • 12,366
  • 5
  • 48
  • 83
0
votes
1 answer

Sanity posts display all categories, not just ones associated with post

I am working on making a blog using React and Sanity, and I want to display the categories associated with each post underneath each post. I am able to display the categories, but it displays all categories, not just the ones associated with each…
Brandon Pyle
  • 307
  • 3
  • 14
0
votes
1 answer

Sanity.io groq filter inside an array of objects using a reference field

I've been stuck for a while trying to optimise my groq query. I have page content that contains an array objects (different languages). I've been playing around in Sanity Vision to see how I can filter the output so that I only get the content in…
Tanpo
  • 233
  • 3
  • 18
0
votes
2 answers

Sanity & GROQ - How to fetch items that does not have a category?

I'm trying to access all the items that does not have Featured in their category array. The GROQ code I use is `*[_type == "post" && *[_type == "category" &&title != "Featured"][0]._id in categories[]._ref] {title, 'mainImage' :…
Sachin Titus
  • 1,960
  • 3
  • 23
  • 41