payloadcms / payloadcms/payload

Query `{ exists: true }` not working as expected for empty relationships

Open
#12,072 9 comments 1 reaction 1 assignee View on GitHub

@JarrodMFlesch is already working on this.

Since Feb 21, 2026.

Bug stale
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
  1. Create a relationship to a tag (hasMany: true).
  2. Save the document.
  3. Remove the relationship.
  4. Save the document.
  5. Query with {exists: true}
  6. 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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.