Project Fields to Return Certain fields from Query

default discord avatar
prakash169212 months ago
5

I am trying to find a way to Project certain fields while querying documents.


Similar to this down below



all fields => {id, title, status, ...otherfields}


Just want these fields => {id, title, status}



Is it possible to achieve this behavior with payload?



//similar example behaviors on MongoDB


const cursor = db


.collection('collection_name')


.find({


status: 'A'


})


.project({ title: 1, status: 1 });



Thank you

  • discord user avatar
    patrikkozak
    12 months ago

    Hey @prakash1692 - You could do this with a GraphQL query!



    Something like:



    query Projects {
      Projects {
        docs {
          id
          title
          status
        }
      }
    }


    Let me know if this makes sense or if you have any questions!

  • default discord avatar
    prakash169212 months ago

    Thankyou for your response. I am specially looking to do with Rest API if there is a way to do it?

  • discord user avatar
    jarrod_not_jared
    12 months ago

    You could use an afterRead hook that strips back the data before passing it to the client. If you do do that I would do it with some sort of query param so you can opt in/out of the trimmed back data



    But “projection” is not supported, no

  • default discord avatar
    prakash169212 months ago

    Thankyou.

  • default discord avatar
    palakrukhaya9 months ago

    @jarrod_not_jared is there any example?? Thanks!

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.