Improve MongoDB join performance: move $sort and $limit before $lookup in generated aggregation #10165
timurvafin
started this conversation in
Feature Requests & Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
When using a "join" field type in Payload CMS (for example, referencing "documents" from "persons"), the generated MongoDB aggregation pipeline places $lookup before $sort, $skip, and $limit. This causes a full collection scan (COLLSCAN) in MongoDB, making queries very slow for large collections.
Current Behavior
Payload CMS generates a pipeline like:
Because $lookup occurs first, MongoDB has to scan the entire collection before sorting and limiting, which often results in a COLLSCAN.
Why It Is a Problem:
Expected/Desired Behavior
The pipeline should apply $sort, $skip, and $limit before $lookup. For example:
In this scenario, only the required 10 "persons" documents are fetched (utilizing an index on createdAt, if present), and then we do $lookup to retrieve related "documents." This avoids a full collection scan and dramatically improves performance.
Steps to Reproduce
Proposed Solution
Additional Context
Thank you!
I appreciate your work on Payload CMS. If there is anything else I can provide (logs, a reproducible test repo, etc.), please let me know. I’m also open to creating a PR if that would help!
Beta Was this translation helpful? Give feedback.
All reactions