Skip to content

fix(db-mongodb): hasMany relationship filtering with equals operator returns no results#15204

Merged
PatrikKozak merged 4 commits intomainfrom
fix/mongodb-hasmany-relationship-equals-filter
Jan 15, 2026
Merged

fix(db-mongodb): hasMany relationship filtering with equals operator returns no results#15204
PatrikKozak merged 4 commits intomainfrom
fix/mongodb-hasmany-relationship-equals-filter

Conversation

@PatrikKozak
Copy link
Copy Markdown
Contributor

@PatrikKozak PatrikKozak commented Jan 14, 2026

What

Fixes filtering hasMany relationship fields (both polymorphic and non-polymorphic) with the equals and not_equals operators in the MongoDB adapter.

When using the WhereBuilder UI to filter by these fields, queries would return zero results instead of matching documents.

Why

The MongoDB adapter's sanitizeQueryValue function wasn't handling array values properly for equals/not_equals operators on hasMany relationship fields.

The values coming from the UI are string IDs that need to be converted to MongoDB ObjectIds before comparison:

// Before fix - strings don't match ObjectIds
{ relationshipHasMany: { $eq: ['507f1f77bcf86cd799439011', '507f191e810c19729de860ea'] } } 
// After fix - converted to ObjectIds
{ relationshipHasMany: { $eq: [ObjectId('507f1f77bcf86cd799439011'), ObjectId('507f191e810c19729de860ea')] } } 

How

Added handling in sanitizeQueryValue.ts to detect when:

  • The operator is equals or not_equals
  • The value is an array
  • The field has hasMany: true

For these cases, the array values are converted to the proper ID types (ObjectId for MongoDB IDs, or custom ID types like numbers) before the query is built.

Note: This fix is MongoDB-specific. Exact array equality filtering for hasMany relationships is not currently supported in SQL adapters (Drizzle/Postgres).

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Jan 14, 2026

📦 esbuild Bundle Analysis for payload

This analysis was generated by esbuild-bundle-analyzer. 🤖
This PR introduced no changes to the esbuild bundle! 🙌

Copy link
Copy Markdown
Contributor

@DanRibbens DanRibbens left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The feedback applies in all tests.

Comment thread test/fields/int.spec.ts
Comment thread test/fields/int.spec.ts
@PatrikKozak PatrikKozak merged commit 1756c0d into main Jan 15, 2026
101 checks passed
@PatrikKozak PatrikKozak deleted the fix/mongodb-hasmany-relationship-equals-filter branch January 15, 2026 16:03
@github-actions
Copy link
Copy Markdown
Contributor

🚀 This is included in version v3.72.0

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants