Skip to content

Commit

Permalink
fix: treat empty strings as null / undefined for exists queries (#8336
Browse files Browse the repository at this point in the history
)

Fixes #7714
  • Loading branch information
PatrikKozak authored Sep 20, 2024
1 parent c86526b commit 31d0b30
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions packages/db-mongodb/src/queries/sanitizeQueryValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,32 @@ export const sanitizeQueryValue = ({
$regex: formattedValue.replace(/[\\^$*+?.()|[\]{}]/g, '\\$&'),
}
}

if (operator === 'exists') {
formattedValue = formattedValue === 'true' || formattedValue === true

if (formattedValue) {
return {
rawQuery: {
$and: [
{ [path]: { $exists: true } },
{ [path]: { $ne: null } },
{ [path]: { $ne: '' } },
],
},
}
} else {
return {
rawQuery: {
$or: [
{ [path]: { $exists: false } },
{ [path]: { $eq: null } },
{ [path]: { $eq: '' } }, // Treat empty string as null / undefined
],
},
}
}
}
}

if (
Expand Down

0 comments on commit 31d0b30

Please sign in to comment.