Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Join field cannot query more than 1 doc with [in] operator using mongodb #8901

Closed
JessChowdhury opened this issue Oct 28, 2024 · 3 comments · Fixed by #8910
Closed

Join field cannot query more than 1 doc with [in] operator using mongodb #8901

JessChowdhury opened this issue Oct 28, 2024 · 3 comments · Fixed by #8910
Assignees

Comments

@JessChowdhury
Copy link
Member

JessChowdhury commented Oct 28, 2024

Describe the Bug

This is the underlying issue for bug #8836.

The following request fails when there is more than one id.

Fails (returns 0 docs):

GET http://localhost:3000/api/join-docs?where[id][in]=[id-one,id-two]

Works (returns 1 doc):

GET http://localhost:3000/api/join-docs?where[id][in]=[id-one]

The error can be tracked down to the result on line 117 packages/db-mongodb/src/find.ts.

This result is generated with packages/db-mongodb/src/utilities/buildJoinAggregation.ts and within here I believe we are querying incorrectly. Unsure of the fix, it might be version compatibility or needing to append the query with $expr.

Link to the code that reproduces this issue

branch issue/join-query (https://github.com/payloadcms/payload/tree/issue/join-query)

Reproduction Steps

  1. Run joins test suite pnpm dev joins
  2. Open Relationship Docs collection
  3. In the list view there are 2 seeded documents, the join relationship field will be untitled
  4. Look at the request in the network tab, returns 0 docs
  5. Delete one of the docs in the collection
  6. Look at the request in the network tab, returns doc successfully
  7. Notice the relationship field cell has the correct title instead of untitled

Which area(s) are affected? (Select all that apply)

db-mongodb

Environment Info

Payload: beta@latest
Node.js: v20.9.0
Next.js: v15.0.0

@r1tsuu
Copy link
Member

r1tsuu commented Oct 28, 2024

PR is here #8910! Thanks!
Merged to your branch, tested:

Image

DanRibbens pushed a commit that referenced this issue Oct 28, 2024
### What?
Fixes the issue with `in` querying when the collection has a join field.

### Why?
When using `.aggregate`, MongoDB doesn't cast a comma delimited value
for the `$in` operator to an array automatically as it's not handled by
Mongoose.

### How?
Sanitizes the incoming value to an array if it should.

Fixes #8901
@r1tsuu r1tsuu closed this as completed Oct 28, 2024
Copy link

🚀 This is included in version v3.0.0-beta.120

Copy link

This issue has been automatically locked.
Please open a new issue if this issue persists with any additional detail.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 30, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants