[Ents]: Ordering on Edge

Hello all, I have a question which in short is whether (or rather how) it's possible to order the query of many:many edges on the other end of the edge using convex-ents. I have user ents which can "attend" events ents - i.e. many:many relationship. I want to query the events a single user is attending and in the same query order the retrieved events based on the start date of the events (in the db as a string, like 2025-01-24T12:30:00.000+01:00) as follows:
await table("users")
.getX(userId)
.edge("attendingEvents")
.order("asc", "by_startdate");
await table("users")
.getX(userId)
.edge("attendingEvents")
.order("asc", "by_startdate");
The events schema contains an index for "startdate". But my retrieved results are never ordered by the startdate. My current solution looks like this:
const events = await table("users")
.getX(userId)
.edge("attendingEvents");

return events
.filter(({ startdate }) => startdate >= today.toISOString())
.sort((a, b) => a.startdate.localeCompare(b.startdate))
.splice(0, take);
const events = await table("users")
.getX(userId)
.edge("attendingEvents");

return events
.filter(({ startdate }) => startdate >= today.toISOString())
.sort((a, b) => a.startdate.localeCompare(b.startdate))
.splice(0, take);
Perhaps there is a more efficient way to do this since I'm filtering out the results that have a startdate in the past anyway. Any help would be highly appreciated 🙂
3 Replies
Convex Bot
Convex Bot5w ago
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!
ampp
ampp5w ago
this is where some extra indexs were added by Michal so we discussed the new feature and accessing the table directly as a hack: https://discord.com/channels/1019350475847499849/1262274896860549181 I'm immediately wondering why your table is .edge("attendingEvents") and not .edges("attendingEvents"), typo?
Michal Srb
Michal Srb5w ago
If you want to filter a query of some documents by some field efficiently, the field must be inside those documents. In this case you are querying the users_to_events documents, but those don't have the start_date. Since we don't have support for storing additional data on the many:many edges (see recent #convex-ents discussion), you must model this manually, via two 1:many edges, and duplicate the start date on the relation. users -> attended 1:many attended -> events many:1 attended.startDate === event.startDate

Did you find this page helpful?