breadcrumbs query using nested docs

default discord avatar
olarssony
last month
4

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
    zoul0813
    last month

    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
    olarssony
    last month

    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
    Payload Team
    last month

    Hey @olarssony glad this was sorted out!



    Thanks @zoul0813 !

  • default discord avatar
    fatso8855
    last month

    This is related to a question I just posted here

    https://discord.com/channels/967097582721572934/1162319476969189456
Open the post
Continue the discussion in Discord
Like what we're doing?
Star us on GitHub!

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

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