payloadcms / payloadcms/payload
Query `{ exists: true }` not working as expected for empty relationships
@JarrodMFlesch is already working on this.
Since Feb 21, 2026.
- Dominant language
- TypeScript
- Stars
- 44.8k
- Forks
- 4.2k
- Avg merge
- 2d 21h
- Merged PRs (30d)
- 53
Description
Describe the Bug
I'm trying to query only documents that have a tag. Which works fine, but if you add a tag, save the document, then remove the tag and save the document, though the tags are empty in the database it's [] which allows { exists: true } to also work.
I've also tried:
or: [{ tag: { exists: true }}, { tag: {not_equals: [] }}] and { 'tag.title': { exists: true } }
Which doesn't work.
After trying many things, it doesn't seem possible to filter out empty relationships without introducing a new field to track the value on hooks.
Link to the code that reproduces this issue
pnpx create-payload-app@latest -t blank
Reproduction Steps
- Create a relationship to a tag (hasMany: true).
- Save the document.
- Remove the relationship.
- Save the document.
- Query with
{exists: true} - The document will still be returned.
Which area(s) are affected? (Select all that apply)
area: core
Environment Info
Payload: 3.33.0
NodeJS: 22.0
NextJS: 15.3.0
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.