Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

breadcrumbs query using nested docs

default discord avatar
olarssonylast year
7

im using the nested docs plugin to create pages, now i want to get a page based on the url it has, which basically means the last item in the breadcrumbs array that is created. the ai-help suggested this query:



/api/collection?where[arrayField][$last][equals]=desiredValue



which looks like this for me:



/api/mainPages?where[breadcrumbs.url][$last][equals]=/projekt



this simply doesnt work, however removing [$last] does filter on the breadcrumbs.url, but it does so on all the fields.



what is the correct query to use here?

  • default discord avatar
    zoul0813last year

    I use the following query to retrieve documents by path

    where[breadcrumbs.url][equals]=/path/to/slug&where[slug][equals]=slug


    Or, put another way ...

    const query = {
    
      'breadcrumbs.url': {
        equals: `/${pathArray.join('/')}`,
      },
      'slug': {
        equals: pathArray.slice(-1)[0],
      },
    };


    the end result here is that you should not have multiple documents that match both the breadcrumb url (at any level of the array), and the slug ...



    If your URL's look like

    /foo/foo/foo/foo

    then you'll have problems here ... as a request for

    /foo/foo

    will match all children as well, the requesting code would then need to go through the array of docs returned and find the appropriate match ... but hopefully, your URL structure is sane and doesn't contain repetitions like this.

  • default discord avatar
    olarssonylast year

    this make perfect sense, ill try this, thanks david



    this solved it, where[breadcrumbs.url][equals]=/path/to/slug&where[slug][equals]=slug



    you saved my day

  • discord user avatar
    seanzubrickas
    last year

    Hey

    @1039510116476649502

    glad this was sorted out!



    Thanks

    @131800639293358080

    !

  • default discord avatar
    fatso8855last year

    This is related to a question I just posted here

    https://discord.com/channels/967097582721572934/1162319476969189456
  • default discord avatar
    dukan9243last year

    Hi guys, sorry to re-open that issue but I am facing a similar problem with the nested docs plugin.



    I have a nested collection (called page-categories) and setting it up and creating nested categories work fine.



    I am trying to query it and can't get it to work.



    I am doing the query proposed here in this thread, but I only get an error "Something went wrong".



    So what I am doing is:



    ?where[breadcrumbs.url][equals]=something



    This is already failing for me with above error. Is it because "breadcrumbs" is an array and I cannot simply call "url" on that array field? How do I query an array field actually? Is that even possible?



    Hopefully somebody can help with this!

  • default discord avatar
    acemaker0012 months ago

    I'm having trouble with this as well.

  • default discord avatar
    isaiahpfisher7 months ago

    If anyone stumbles across this thread in the future and needs an alternative solution, here's what I ended up doing. It might not be ideal, but it should work:



    Using a

    beforeValidate

    or

    beforeChange

    hook on your collection, you can add a readOnly

    url

    field to the collection. The hook can automatically generate the url using the breadcrumbs/slug and always keep it up-to-date. Then it's super easy to query by the url field.

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.