Query syntax issue resolved with assistance from the community.
18 replies
Last updated: Dec 14, 2022
E
Hi all, I was wondering if I could get help with a specific type of query. In the following sample, I'm trying to get all the authors from a particular publisher but I can't figure out the exact GROQ syntax. Thanks much in advance!
Sample query:
Sample expected result (pseudocode):
Sample schema (pseudocode):
Sample data set (pseudocode):
Sample query:
*[_type == 'publisher' && code == 'PUB1']{ authors: *[_type == 'author' && books[]->publisher._ref == ^._id] }
"result" : [ 0: { authors: [ author1, author2 ] } ]
author: fields: [ { name: 'books', type: 'array', of: [{ type: 'reference', to: [{ type: 'book' }] }] } ] book: fields: [ { name: 'publisher', type: 'reference', to: [{ type: 'publisher' }] } ] publisher: fields: [ { name: 'code', type: 'string' } ]
sample set of authors: [ { _id: 'author1', books: [ { _ref: 'book1' }, { _ref: 'book2' } ] }, { _id: 'author2', books: [ { _ref: 'book3' } ] }, { _id: 'author3', books: [ { _ref: 'book2' } ] } ] sample set of books: [ { _id: 'book1', publisher: { _ref: 'publisher1' } }, { _id: 'book2', publisher: { _ref: 'publisher2' } }, { _id: 'book3', publisher: { _ref: 'publisher1' } } ] sample set of publishers: [ { _id: 'publisher1', code: 'PUB1' }, { _id: 'publisher2', code: 'PUB2' }, { _id: 'publisher3', code: 'PUB3' }, ]
Nov 21, 2022, 4:33 AM
E
Bumping up to see if I could get help here please. We've been stuck for a few days now. Thanks much in advance!
Nov 22, 2022, 10:03 PM
Here's an example of a working query for the data you shared.
Nov 28, 2022, 7:02 PM
E
Ah! Using
ininstead of
==. I'll give it a shot. Sorry for the delayed response. Didn't see this until today. I'll give it a shot and report back. Thanks much as always
user M
!Dec 4, 2022, 7:28 PM
E
user M
So I've got a weird thing going on. I tried applying this to the full dataset. If I explicitly use the _id (e.g., publisher1), it works, but when I use ^._id, the query returns an empty result. Any suggestions?Works:
*[_type == 'publisher' && code == 'PUB1']{ 'authors': *[_type == 'author' && 'publisher1' in books[]->.publisher._ref] }
*[_type == 'publisher' && code == 'PUB1']{ 'authors': *[_type == 'author' && ^._id in books[]->.publisher._ref] }
books[]->publisherand
books[]->.publisherwith the same results.
Thanks much in advance!
Dec 5, 2022, 2:02 AM
E
Bumping up please.
user M
or anyone else who can help. Thanks much in advance!Dec 9, 2022, 4:42 PM
E
2022-10-26
Dec 10, 2022, 3:13 AM
E
Morning
user M
! Sorry for pressing, bumping to the top as the project that this is for is getting pushed harder for completion this week and this is the last piece in our puzzle. Thanks much in advance!Dec 12, 2022, 3:18 PM
I'll need significantly more information to troubleshoot your query for you. If what I worked out for you s not working, then your content model does not match the data structure that you shared.
Dec 12, 2022, 5:25 PM
E
Ok, I can do that. Mind if we DM please?
Dec 12, 2022, 5:56 PM
Please don't DM me. I'm unable to provide support in a private channel. Since we're a community it's important that answers are discoverable for others that may have the same question. If you would like private dedicated support, you can upgrade to an Enterprise plan .
Dec 12, 2022, 7:53 PM
E
Ah, apologies. Noted about the private channel.
We've double and triple-checked multiple times the posted content model, but I'll check again.
As far as Enterprise, we'll have to see. This particular project is a small non-profit already on the non-profit plan, so Enterprise plan will likely be out of scope.
We've double and triple-checked multiple times the posted content model, but I'll check again.
As far as Enterprise, we'll have to see. This particular project is a small non-profit already on the non-profit plan, so Enterprise plan will likely be out of scope.
Dec 12, 2022, 7:57 PM
If your dataset is public, you can share the url that Vision outputs for the query. If not, I can add myself to your project and see if I can get it to work in Vision.
Dec 12, 2022, 7:59 PM
E
Ok, that works. Let me see what our setup is.
Dec 12, 2022, 8:00 PM
E
Ok, checked it out. Our dataset is private. How do I share the URL here safely?
Dec 13, 2022, 12:46 AM
Go it! If it's private, you can either share your project ID and I'll add myself to the project, or I can DM you my email address and you can invite me. Whichever is more comfortable for you.
Dec 13, 2022, 2:07 AM
E
Sure thing, I can invite you.
Dec 13, 2022, 2:07 AM
E
Got it! My problem was that in Vision, I was using an old API version. Switching to a newer version made the query work. Ultimately the suggested code worked:
Thanks much for the assist
*[_type == 'publisher' && code == 'PUB1']{ 'authors': *[_type == 'author' && ^._id in books[]->.publisher._ref] }
user M
!Dec 14, 2022, 11:58 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.