sanity documents query *[_id in path("/somePath")] currently returning bad pattern

4 replies
Last updated: Oct 1, 2020
Morning, all! Any GROQ folks out there who can tell me what’s wrong with my query? Stumped!
sanity documents query *[_id in path("/somePath")]
currently returning
bad pattern: *[_id
AI Update

The issue with your query is that the command-line is interpreting the parentheses in path("/somePath") as special shell characters. You need to escape or quote your query properly.

Here are a few solutions:

Option 1: Use single quotes around the entire query

sanity documents query '*[_id in path("/somePath")]'

Option 2: Escape the parentheses

sanity documents query "*[_id in path\(\"/somePath\"\)]"

Option 3: Use double quotes and escape inner quotes

sanity documents query "*[_id in path(\"/somePath\")]"

The path() function in GROQ is really useful for filtering documents by their ID path. Here's how it works:

  • *[_id in path("a.b.c.*")] matches IDs like a.b.c.d but not a.b.c.d.e (single level)
  • *[_id in path("a.b.c.**")] matches a.b.c.d and also deeper paths like a.b.c.d.e.f.g (all levels)
  • *[!(_id in path("a.b.c.**"))] matches anything NOT under that path

You can also use path() for common patterns like getting all drafts:

*[_id in path("drafts.**")]

The shell escaping issue is a common gotcha when running GROQ queries from the command line - always wrap your queries in quotes to prevent the shell from interpreting special characters! The single quotes option (Option 1) is usually the safest bet since it prevents the shell from interpreting anything inside.

The query needs to be a string
sanity documents query "*[_id in path('somePath')]"

Also you can't have slashes in your path, I think
The query needs to be a string
sanity documents query "*[_id in path('somePath')]"

Also you can't have slashes in your path, I think
🤦
Thanks
user J

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?