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.

Project Fields to Return Certain fields from Query

default discord avatar
prakash1692last year
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
    last year

    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
    prakash1692last year

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

  • 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
    prakash1692last year

    Thankyou.

  • default discord avatar
    palakrukhayalast year

    @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 dedicated engineering support directly from the Payload team..