Querying nested-docs

default discord avatar
Davidlast year
4

What is the recommended way of querying nested-docs when you know the path?



Given the following paths:


/parent-page
/parent-page/child-page
/parent-page/child-page/grandchild-page


/api/pages?where[breadcrumbs.url][equals]=/parent-page/child-page



This query returns both the child-page document, and the grandchild-page document ...



I'm having trouble figuring out how to retrieve only the page that matches this path exactly. I am temporarily using a hidden auto-generated field, that takes the last breadcrumb and stores it's

url

into a

path

field at the document root ... so I can query

where[path][equals]=/parent-page/child-page

but this seems awkward, and I'm assuming there's a proper way to query nested-docs?

  • discord user avatar
    jacobsfletch
    last year

    That url probably fails because the forward slashes in your

    where

    query are not encoded, which would look like this:

    /api/pages?where[breadcrumbs.url][equals]=%2Fparent-page%2Fchild-page


    But this may match on multiple docs instead of just one, because the breadcrumb may appear across more than one document, especially within the same tree.



    You may want to query by

    slug

    then find the document whose

    last

    breadcrumb matches your url

  • default discord avatar
    Davidlast year

    thanks - I think that helped. I just added the slug to the query and that seems to have resolved it ...



    I now have something like this ...



    ?where[breadcrumbs.url][equals]=/new-page/child-page/grandchild-page&where[slug][equals]=grandchild-page


    The URL does get encoded by the browser before sending, I'm just showing the decoded version for readability.

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.