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
1
vote
1 answer

Sanity.io GROQ: retrieve documents that have a key defined inside array of objects

I have documents that look like this { "_type": "celeb", "name": "foo", "facts: [ {a: 1, topics: {_ref: 'asd', _type: 'reference'}}, {a: 2}, {a: 3, topics: {_ref: 'dfg', _type: 'reference'}} ] } Not all documents have facts that…
M.K. Safi
  • 6,560
  • 10
  • 40
  • 58
1
vote
1 answer

How to compare arrays in GROQ

I have a blog with sanity CMS and I want to request related posts depending on corresponding tags from the current post which means I have to compare two arrays of tags. My tags are described in post schema this way: { name: 'tags', title:…
Quentin C
  • 307
  • 3
  • 13
1
vote
1 answer

Limit array of references to documents that have not been referenced yet

I have a "reference selector" (i.e. a module where editors can choose documents to reference) but I want to limit the results to the documents that have not been referenced yet. This is the schema of my reference selector that is used inside another…
Christoph Berger
  • 341
  • 1
  • 4
  • 16
1
vote
1 answer

How to show Referenced Object with Groq

I have a "Success cases" page (/cases) where when I go into the detail page I have something like /cases/some-slug. The "cases" schema is referencing to a Brand, like this: {title: 'Case Brand', name: 'caseBrand', type: 'reference', …
1
vote
3 answers

Sanity io show blog posts by author

I'm learning Sanity io CMS and GROQ. I've build a simple blog using one the Sanity predefined blog schema. I've managed to show posts, a single post and the author page, showing name, image and bio. What I would like to do is to show a list of the…
Mauro74
  • 4,686
  • 15
  • 58
  • 80
1
vote
1 answer

Groq: join to a document field

I have a schema that reads something like this: a Page (document) has an array of Items (object); each Item can have a Post (document); each Post has several Tags (document). I have got most of the drilling down working, but for some reason I can't…
Eric_WVGG
  • 2,957
  • 3
  • 28
  • 29
1
vote
1 answer

Filter query result by field value inside array of objects [Sanity.io & GROQ]

I'm trying to find a product variant inside my list of products(on sanity.io using GROQ), to do so, I have the sku of the variant that I want. The query I'm using is *[_type == "product" && variants[].sku.current =="kit-kat-wasabi-5" ] But this…
GB5
  • 469
  • 1
  • 5
  • 16
1
vote
1 answer

Can I reach a grandparent from a GROQ query?

I'm trying to run a GROQ query for my Sanity.io project that should filter out child documents that contain references based on a grandparents id. Is it possible to do so? I've come to an understanding that for similar scenarios I could use the…
cabc
  • 35
  • 1
  • 7
1
vote
1 answer

Fetch _id of last created document of given type in Sanity

In Sanity, for a given document type named message, how can I get the _id of the newest message document?
ArneHugo
  • 6,051
  • 1
  • 26
  • 47
0
votes
0 answers

How can I fetch a data "streak" using GROQ in a Sanity data set?

I used Sanity.io to create a way to manually track details of a game that my brother and I are playing and I plan to create a leaderboard of our versus stats and significant milestones. I'm a bit new to Svelte but thought this would be a fun pet…
0
votes
0 answers

Filtering localized content (Sanity / groq / typescript (Document Internationalization)

Could somebody please help me out with the following? Have a data set ('legal') in Sanity which contains a document-specific language field ('en'/'nl' etc (string 'language')). This is part of the document-internationalization plug-in and will be…
0
votes
0 answers

Groq query to get two different quantity of data from same document on a condition

I have a document which has many properties. There is also a boolean in all the sub documents called featured. Now I want to make a query that would return 2 documents with featured being true and 6 documents with featured being false. Currently my…
Rao Asim
  • 158
  • 8
0
votes
0 answers

How can I check if a document has an object with the same key and value as an array of objects?

I have a product document, and each product has an array of "productFilters", with this shape: { name: "Filter Name", value: "Filter Value", } For my params, I have an array of key/value pairs, and I need to check if any of the product filters…
greatbearshark
  • 165
  • 1
  • 2
  • 8
0
votes
0 answers

Sanity GROQ Referencing

Inside of Sanity I have setup pages that have an array of page sections - a page sections is a section of the page that allows me to build dynamic pages by adding and organizing page sections however I want. Some sample page sections are:…
execv
  • 822
  • 1
  • 8
  • 23
0
votes
0 answers

How can I include a referenced field inside a GROQ `score()` function?

I have a "posts" data type, with an author field on each post that links the post to an authors data type. How can I rank with the score() function posts that have a certain author? On Sanity's website, the developers state: score() cannot take a…