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 ?
Jul 31, 2020, 11:55 PM
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"
]

Aug 1, 2020, 7:06 AM
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"
Aug 1, 2020, 7:09 AM
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 ?
Aug 1, 2020, 8:30 AM
Yup! You can pretty much filter by any field and data whenever it is available in the datastore
Aug 1, 2020, 8:32 AM
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)
Aug 1, 2020, 8:33 AM
Amazing stuff. Go GROQ, keep going !!!
Aug 1, 2020, 8:34 AM
*[
  _type == "post" &&
  count(authors) > 1 &&
  authors[]->state match "California"
]

☝️ I believe the latter would be like this
Aug 1, 2020, 8:34 AM
In other words: GROQ lets you query any JSON structure without needing a schema, since it parses the actual data
Aug 1, 2020, 8:35 AM
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 🙂
Aug 1, 2020, 8:35 AM
I wish! (I've just being doing this for 2 years and was by happen stance here)
Aug 1, 2020, 8:36 AM
(and figuring out GROQ queries for stuff is always fun)
Aug 1, 2020, 8:37 AM
i can understand by seeing the query, it's very addictive. And it fetched in 196 ms, which is really impressive too
Aug 1, 2020, 8:38 AM
Should be even faster if you point it to the CDN
Aug 1, 2020, 8:38 AM
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) ?
Aug 1, 2020, 8:45 AM
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
Aug 1, 2020, 8:51 AM
Very cool ! You guys really deserve a word of mouth https://twitter.com/iamvajid/status/1289485442983661570
Aug 1, 2020, 8:58 AM
Appreciate it!
Aug 1, 2020, 8:58 AM
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..
Aug 1, 2020, 2:37 PM

Sanity– build remarkable experiences at scale

Sanity is a modern 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?