Commit 1756c0d
authored
fix(db-mongodb): hasMany relationship filtering with equals operator returns no results (#15204)
### 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).1 parent f98d915 commit 1756c0d
3 files changed
Lines changed: 383 additions & 0 deletions
File tree
- packages/db-mongodb/src/queries
- test/fields
- collections/Relationship
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
307 | 307 | | |
308 | 308 | | |
309 | 309 | | |
| 310 | + | |
| 311 | + | |
310 | 312 | | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
311 | 355 | | |
312 | 356 | | |
313 | 357 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
726 | 726 | | |
727 | 727 | | |
728 | 728 | | |
| 729 | + | |
| 730 | + | |
| 731 | + | |
| 732 | + | |
| 733 | + | |
| 734 | + | |
| 735 | + | |
| 736 | + | |
| 737 | + | |
| 738 | + | |
| 739 | + | |
| 740 | + | |
| 741 | + | |
| 742 | + | |
| 743 | + | |
| 744 | + | |
| 745 | + | |
| 746 | + | |
| 747 | + | |
| 748 | + | |
| 749 | + | |
| 750 | + | |
| 751 | + | |
| 752 | + | |
| 753 | + | |
| 754 | + | |
| 755 | + | |
| 756 | + | |
| 757 | + | |
| 758 | + | |
| 759 | + | |
| 760 | + | |
| 761 | + | |
| 762 | + | |
| 763 | + | |
| 764 | + | |
| 765 | + | |
| 766 | + | |
| 767 | + | |
| 768 | + | |
| 769 | + | |
| 770 | + | |
| 771 | + | |
| 772 | + | |
| 773 | + | |
| 774 | + | |
| 775 | + | |
| 776 | + | |
| 777 | + | |
| 778 | + | |
| 779 | + | |
| 780 | + | |
| 781 | + | |
| 782 | + | |
| 783 | + | |
| 784 | + | |
| 785 | + | |
| 786 | + | |
| 787 | + | |
| 788 | + | |
| 789 | + | |
| 790 | + | |
| 791 | + | |
| 792 | + | |
| 793 | + | |
| 794 | + | |
| 795 | + | |
| 796 | + | |
729 | 797 | | |
730 | 798 | | |
731 | 799 | | |
| |||
0 commit comments