How to tell the query to include specific fields
Instead of returning ALL of the fields in a document I want the query to only include specific fields. When I asked AI to help it said to put a .map function after the .query but when I do that I get a .map is not a function error.
Ideally I want to specify the fields in the query, not in the return. Akin to select firstname, lastname from people where... as opposed to what is happening now which is select * from people where...
What I tried based on AI.
export default query(async (ctx) => {
console.log("Write and test your query function here!");
return await ctx.db.query("people").map((person) => ({
nameFirst: person.name_first
})).collect();
})
What get - Server Error
Uncaught TypeError: ctx.db.query(...).map is not a function6 Replies
Thanks for posting in <#1088161997662724167>.
Reminder: If you have a Convex Pro account, use the Convex Dashboard to file support tickets.
- Provide context: What are you trying to achieve, what is the end-user interaction, what are you seeing? (full error message, command output, etc.)
- Use search.convex.dev to search Docs, Stack, and Discord all at once.
- Additionally, you can post your questions in the Convex Community's <#1228095053885476985> channel to receive a response from AI.
- Avoid tagging staff unless specifically instructed.
Thank you!
Queries (ctx.db.query) can only return whole documents, you can map after retrieving the documents.
Doesn't that seem like it would be a hit on efficiency?
I'm mean if that's the way it is, then that's the way it is. I can work with it. Just seems like a lot of work on the other end to filter out. In the thing you have been helping me with, I want to limit the fields returned, but it seems to break the stacked/nested queries.
A lot of the trip in going from SQL to Convex is things seeming problematic or inefficient.
The js/ts api in your Convex functions is very low level, and the code is running very close to the database.
My main encouragement to devs getting into Convex is to just focus on outcomes, don't worry about performance until there's a problem. It's fast by default, like really fast.
OK. I've taken what you've given me, which works thanks again, and reworking it from the outside in to both learn how/why it works, and to customize it a bit more. Agreed, it is very zippy.
Glad to hear it!