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
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!
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
Thankyou.
@jarrod_not_jared is there any example?? Thanks!
Star
Discord
online
Get dedicated engineering support directly from the Payload team..