Like what we’re doing? Star us on GitHub!

Does anyone know if the Payload API can return multiple items in one request?

MRL
4 weeks ago
14

E.g. If I want to fetch all pages and all users. Could that be done with one API call?

  • jmikrut
    Payload Team
    4 weeks ago

    đź‘Ť



    ok so there are a few options



    short answer, Payload doesn't do anything like that out of the box



    but longer answer - you could open up a custom endpoint and use a MongoDB

    aggregate

    query that merges content from multiple collections into a single result set



    OR, you could build some type of "search" collection



    take a look at hope network's search functionality:



    https://hopenetwork.org/


    this site uses a collection called

    Search
  • MRL
    4 weeks ago

    Ok thanks. I will investigate the custom endpoint route with MongoDb.

  • jmikrut
    Payload Team
    4 weeks ago

    and then every collection that we want to search across has an

    afterChange

    hook, that syncs it to the

    search

    collection



    so then the frontend of the hope site just queries against the

    search

    collection, and then documents from like 6 collections all get returned



    in a very fast / performant way



    this is good if you just need a quick solution. but another thing to look into, if you are going to be doing heavy searches, and you need it to scale with thousands of docs, is to use Algolia or similar

  • MRL
    4 weeks ago

    Cool, cheers @jmikrut

Open the post
Continue the discussion in Discord
Can't find what you're looking for?
Get help straight from the Payload team with an Enterprise License.Learn More