Querying multiple authors in Sanity using GROQ

19 replies
Last updated: Aug 1, 2020
Lets say i have "Posts" and i have "Authors" which is multiple authors for a post. Is it possible to do a query for "Give me all posts that are written by more than one author, where Chris is the second author" (as mentioned in the"shoptalkshow") ? What does that query look like ?
AI Update

Yes! This is absolutely possible in GROQ. You can access array elements by their index position and filter on specific positions within an array.

Here's how you would write that query:

*[_type == "post" && count(authors) > 1 && authors[1]->name == "Chris"]

Let me break down what's happening:

  1. count(authors) > 1 - Filters for posts with more than one author
  2. authors[1] - Accesses the second element in the authors array (arrays are zero-indexed, so [1] is the second position)
  3. ->name == "Chris" - Dereferences the author reference and checks if the name equals "Chris"

If you want to return specific fields from the matching posts, you can add a projection:

*[_type == "post" && count(authors) > 1 && authors[1]->name == "Chris"]{
  title,
  authors[]->{name}
}

A few important notes:

  • Array indexing is zero-based: authors[0] is the first author, authors[1] is the second, etc.
  • The -> operator dereferences the reference to get the actual author document
  • You can chain conditions: Combine multiple filters with && to get exactly what you need

If your authors array contains objects rather than references, you'd adjust slightly:

*[_type == "post" && count(authors) > 1 && authors[1].name == "Chris"]

This pattern works for any array position - just change the index number to match the position you're interested in!

This is a fun challenge! Here is how it actually looks like for the sanity blog, using “Espen” as the second author:
*[
  _type == "post" &&
  count(authors) > 1 &&
  authors[1]->name match "Espen"
]

So
authors[1]->name match "Espen"
reads: “Pick the second item (array position 1) object in the authors array
[1]
, and follow its reference
->
to the author document, and check if its name
match
the string
"Espen"
OMG, that's bloody awesome 🙂 Really impressed!
Does it work on other attributes of the author also, let's say, author has a "city" property, and if i ask the same question, but instead of name of the author, i would say, authors from "California". Does it still work (Yes yes, trying to battle test the flexibility) ? And does the data structure work even when the referenced author updates the address ? is that with an eventual consistency ?
Yup! You can pretty much filter by any field and data whenever it is available in the datastore
Here’s a playground with a few links as well: https://groq.dev/
(there are some few specifics when it comes to querying the sanity datastore vs using groq-js, we are working on getting those aligned)
Amazing stuff. Go GROQ, keep going !!!
*[
  _type == "post" &&
  count(authors) > 1 &&
  authors[]->state match "California"
]

☝️ I believe the latter would be like this
In other words: GROQ lets you query any JSON structure without needing a schema, since it parses the actual data
btw, are you guys using any AI engine with webhooks and realtime listners/responders ? Your response time is making me think that you are a bot 🙂
I wish! (I've just being doing this for 2 years and was by happen stance here)
(and figuring out GROQ queries for stuff is always fun)
i can understand by seeing the query, it's very addictive. And it fetched in 196 ms, which is really impressive too
Should be even faster if you point it to the CDN
Just curious about two things here:1. Are you guys a mongo shop ? To have this quick response time on relational (referenced) fields querying, are you keeping a denormalized structure for all data?
2. As you have open-sourced GROQ (which is a very generous step), does it allow me to get similar capability on my custom JSON based data store also, even if i have a "reference/relation" in mongo (or any other database) ?
1. Nope, the datastore is built by us using boring mature database technologies like Postgres and elastic. The references are indexed (so that we can prevent deletion of a document with references to it). 2. Yes, I believe it does (under the MIT license). Not saying it would be easy though
😄 We also wrote and published the parser-compiler that we used to make groq-js: https://www.sanity.io/blog/why-we-wrote-yet-another-parser-compiler#glush-the-project-6541c09fa22b . We have started to see some non-sanity adoption for groq-js, like https://github.com/kmcaloon/gatsby-plugin-groq and https://sanity-io-land.slack.com/archives/C9YQ161LZ/p1593101021304400
Very cool ! You guys really deserve a word of mouth https://twitter.com/iamvajid/status/1289485442983661570
Appreciate it!
I fully agree! Sanity has been a joy to work with up until now. Such a shame I’ve only found out about it recently..

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?