Skip to content

Conversation

@r1tsuu
Copy link
Member

@r1tsuu r1tsuu commented Oct 24, 2024

Uses lookups for relationships querying (where { relationship.name }) of subquerying.

Before, whenever we needed to query by a property from another document, we did this:

const result = await SubModel.find(subQuery, subQueryOptions)

const $in: unknown[] = []

result.forEach((doc) => {
  const stringID = doc._id.toString()
  $in.push(stringID)

  if (mongoose.Types.ObjectId.isValid(stringID)) {
    $in.push(doc._id)
  }
})

Now, we are building an aggregation with lookups to query by those!

Benchmark

Benchmark in test/relationships/int.ts - before https://github.com/r1tsuu/payload/tree/bench/aggregations-before after https://github.com/r1tsuu/payload/tree/bench/aggregations-after

Run with:

pnpm exec cross-env NODE_OPTIONS="--no-deprecation" node 'node_modules/jest/bin/jest.js' '/Users/ritsu/work/payload-3/test/relationships/int.spec.ts' -c '/Users/ritsu/work/payload-3/test/jest.config.js' -t 'Relationships Querying Nested Querying should allow querying several times, one and two level deep'

Code that benchmark uses: https://github.com/r1tsuu/payload/blob/1b8eb31fa56a45316d520e92ddcb2f5c3800a0dd/test/relationships/int.spec.ts#L878-L926

Results:

Before:
image
After:
image

Additionally

  • Ensures we always use the transaction when building the query. We didn't do this before for relationship querying.
  • Unblocks sorting by relationships in mongodb (we support it with postgres/sqlite adapters), can implement in a separate PR.
@r1tsuu r1tsuu marked this pull request as ready for review October 25, 2024 18:26
@r1tsuu r1tsuu force-pushed the perf/use-aggregations-for-rels-querying branch from 2e578b8 to 0b8afaf Compare November 15, 2024 19:52
@vercel
Copy link

vercel bot commented Nov 15, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
template-with-vercel-website ❌ Failed (Inspect) Nov 15, 2024 7:52pm
@denolfe denolfe removed the v3 label Nov 19, 2024
@r1tsuu r1tsuu force-pushed the perf/use-aggregations-for-rels-querying branch from 0b8afaf to 38b8e7c Compare December 8, 2024 22:31
@r1tsuu r1tsuu requested a review from DanRibbens December 9, 2024 15:54
@r1tsuu r1tsuu force-pushed the perf/use-aggregations-for-rels-querying branch from 26d66f8 to 7cab4e6 Compare December 20, 2024 13:56
@r1tsuu r1tsuu force-pushed the perf/use-aggregations-for-rels-querying branch from 0053937 to 010677a Compare December 20, 2024 16:31
DanRibbens pushed a commit that referenced this pull request Apr 10, 2025
### What?
This PR adds support for `where` querying by the join field (don't
confuse with `where` querying of related docs via `joins.where`)

Previously, this didn't work:
```
const categories = await payload.find({
  collection: 'categories',
  where: { 'relatedPosts.title': { equals: 'my-title' } },
})
```

### Why?
This is crucial for bi-directional relationships, can be used for access
control.

### How?
Implements `where` handling for join fields the same as we do for
relationships. In MongoDB it's not as efficient as it can be, the old PR
that improves it and can be updated later is here
#8858

Fixes #9683
@rafaell-lycan
Copy link

rafaell-lycan commented Apr 11, 2025

@r1tsuu any idea when this perf optimization could be released? 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment