payload.find() - Retrieve only certain fields?

default discord avatar
plainnn
3 months ago
107

Is it possible to just get x y z from a collection, rather than the entire document? I've added

depth: 1

but it's still quite a lot of data that isn't just needed. Is it possible to add a mongo query or something? I feel like i've looked through the docs a million times and haven't seen anything... any help would be great

  • default discord avatar
    notchr
    3 months ago

    @plainnn You can add custom endpoints to a collection



    For instance, if you want to add another GET endpoint that only pulls specific fields, etc



    Collections have an "endpoints" property that takes an array of endpoint confiigurations



    let me provide an example

  • default discord avatar
    plainnn
    3 months ago

    Ah nice, i was just thinking of building a next api end point which did something similar



    but keeping it all in payload is much nicer.

  • default discord avatar
    notchr
    3 months ago

    OK



    I just cooked this up, so forgive me if it's a little messy



    It's early



        endpoints: [
        {
          path: "/custom-post",
          method: "post",
          handler: async (req, res, next) => {
            try {
    
              if (!id) throw new Error('ID is required')
              const { id } = req.body;
              const result = await payload.findByID({
                collection: "posts", // required
                id, // required
                // depth: 2,
                // locale: "en",
                // fallbackLocale: false,
                // user: dummyUser,
                // overrideAccess: false,
                // showHiddenFields: true,
              });
    
              // Modify the returned document from the result
    
              delete result.whatever
    
    
              res.json(result);
    
    
            } catch (error) {
              console.log(error);
              res.json({
                ok: false,
                error: "Example error message",
              });
            }
          },
        },
      ],
  • default discord avatar
    plainnn
    3 months ago

    Hahaha, not an issue at all - it's super appreciated!

  • default discord avatar
    notchr
    3 months ago

    errr its supposed to be custom-post



    there we go



    So we check to see if ID was included in the request



    then we pass the ID to payload.findByID



    Then we modify the returned document and send it back

  • default discord avatar
    plainnn
    3 months ago

    Awesome, this is amazing!



    Out of interest, i'm using this code in

    getServerSideProps
  • default discord avatar
    notchr
    3 months ago

    Thanks! Hopefuly that helps!

  • default discord avatar
    plainnn
    3 months ago
    const behindTheScenesPosts = await payload.find({
        collection: 'behind-the-scenes',
        sort: '-date',
        limit: 999,
        depth: 1,
      })
    
      const dates = behindTheScenesPosts.docs.map(post => {
        return post.date
      })


    basically I want the last post date, the most recent date, and then an array of all the dates



    is it more perfomant to do that with an endpoint?

  • default discord avatar
    notchr
    3 months ago

    good questio

  • default discord avatar
    plainnn
    3 months ago

    I was just concerned that getting

    all

    posts server side wasn't ideal

  • default discord avatar
    notchr
    3 months ago

    if we check out the doc for Find



    // Result will be a paginated set of Posts.
    // See /docs/queries/pagination for more.
    const result = await payload.find({
      collection: "posts", // required
      depth: 2,
      page: 1,
      limit: 10,
      where: {}, // pass a `where` query here
      sort: "-title",
      locale: "en",
      fallbackLocale: false,
      user: dummyUser,
      overrideAccess: false,
      showHiddenFields: true,
    });


    There are maybe some ways on here



    im looking at "Sort"



    so if you had two reqs



    -date, date



    and limit it to one



    that's one idea



    Then you'd have 3 reqs tho



    but less data



    another idea is there where clause

  • default discord avatar
    plainnn
    3 months ago

    Yeah that is nice, but I still need an array of

    all

    the dates

  • default discord avatar
    notchr
    3 months ago

    which takes some conditions



    Right



    I'd make the initial call for all the dates



    And IIRC dates are numbers?



    (could be wrong)



    but in any case, you could likely just grab the first and last item if they are sorted

  • default discord avatar
    plainnn
    3 months ago

    From payload-types

    date: string
  • default discord avatar
    notchr
    3 months ago

    Ah

  • default discord avatar
    plainnn
    3 months ago

    I think I still prefer it being an API end point 🤔

  • default discord avatar
    plainnn
    3 months ago

    rather than server side

  • default discord avatar
    notchr
    3 months ago

    So maybe the Date native oculd help



    to get the number, and sort by that, faster than string sort



    but also, sorting shouldn't be too costly



    I think though with this scenario, im not as helpful



    that's more of like a data manip question i think



    Though, maybe another payload user on here could assist with your second question 😄



    I think there should be an easy way of doing it, i just dont know

  • default discord avatar
    plainnn
    3 months ago

    Hahah no that's fine, I can do the getting of dates etc, just want to know what is best with end points or serverSideProps 😄



    You have done plenty!

  • default discord avatar
    notchr
    3 months ago

    Cool cool! Well, always hapy to help, feel free to ping me if you get stuck on anything

  • default discord avatar
    plainnn
    3 months ago
      endpoints: [
        {
          path: '/get-dates',
          method: 'post',
          handler: async (req, res, next) => {
            try {
              const behindTheScenes = await payload.find({
                collection: 'behind-the-scenes',
                depth: 1,
                sort: '-date',
              })
    
              const dates = behindTheScenes.docs.map(behindTheScene => {
                return behindTheScene.date
              })
    
              const firstDate = dates[0]
              const lastDate = dates[dates.length - 1]
    
              return res.json({
                firstDate,
                lastDate,
                dates,
              })
            } catch (error) {
              console.log(error)
              res.json({
                ok: false,
                error: 'Couldnt get dates',
              })
            }
          },
        },
      ],


    Think we're looking good

  • default discord avatar
    notchr
    3 months ago

    Nice!!



    Also, not super necessary, but you also have access to req.params



    if you want it to be a GET req

  • default discord avatar
    plainnn
    3 months ago

    am I right in thinking the end point should be

    /api/behind-the-scenes/get-dates

    ? - not hitting it atm

  • default discord avatar
    notchr
    3 months ago

    right, i was going to mention the url



    I think it's relative to the collection base

  • default discord avatar
    plainnn
    3 months ago

    Yeah, I don't need any params at the moment, but definitely nice

  • default discord avatar
    notchr
    3 months ago

    so yeah, api/behind-the-scenes/get-dates



    (Assuming this collection's url is /api/behind-the-scenes/)

  • default discord avatar
    plainnn
    3 months ago

    Interesting..

    NotFound: The requested resource was not found.


    Yeah, i'm using that for other reqs

  • default discord avatar
    notchr
    3 months ago

    and it was a POST?

  • default discord avatar
    plainnn
    3 months ago

    🤦‍♂️

  • default discord avatar
    notchr
    3 months ago

    hehe

  • default discord avatar
    plainnn
    3 months ago

    that's the issue lmfao

  • default discord avatar
    notchr
    3 months ago

    trust, ive done it 10000 times

  • default discord avatar
    plainnn
    3 months ago

    so easily done!

  • default discord avatar
    notchr
    3 months ago

    😄



    Also, side note, the main payload config also takes an endpoints array



    if you want global routes



    rather than per-collection

  • default discord avatar
    plainnn
    3 months ago

    Oh awesome, that's super nice!



    At my agency i keep saying payload is the gift that keeps on giving 😄



    It's a pretty small request, but it's hard to test vs serverSideProps

    image.png
  • default discord avatar
    notchr
    3 months ago

    I agree! We are using it at work, but i also used it when i was doing freelance too



    That's a very tiny req



    yea

  • default discord avatar
    plainnn
    3 months ago

    I might have a shot at making a mongo db request in a next api end point

  • default discord avatar
    notchr
    3 months ago

    interesting, for custom data?

  • default discord avatar
    plainnn
    3 months ago

    Yeah, just so I can query the dates and only the dates, to get it as small as possible



    It's not an issue at the moment since we have 11 items in the collection, but we plan to have many many many more

  • default discord avatar
    notchr
    3 months ago

    Hmm



    So you think for instance, 1000 items

  • default discord avatar
    plainnn
    3 months ago

    (potentially thousands in yrs to come)

  • default discord avatar
    notchr
    3 months ago

    youll get a larger ms delay



    ?

  • default discord avatar
    plainnn
    3 months ago

    yeah valid, i'm thinking wrong - the response wont change, just the time

  • default discord avatar
    notchr
    3 months ago

    You can also optimize / index collections IIRC, not sure if that is relevant here



    Well time is important but



    If it scales proportionally, then at 10,000 docs thats 190ms

  • default discord avatar
    plainnn
    3 months ago

    😄 - I think we are fine

  • default discord avatar
    notchr
    3 months ago

    which is still not even a quarter of a second



    😄



    I could be wrong though

  • default discord avatar
    plainnn
    3 months ago

    Awesome, i'll leave it at that



    Thanks a lot (not) chris

  • default discord avatar
    notchr
    3 months ago

    Hey of course Plain, feel free to ping me or just say hey to talk about web dev!



    Thanks for checking out Payload, the dev team is super rad 😄

  • default discord avatar
    plainnn
    3 months ago

    I will for sure 🙌

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.