Skip to content

Commit e916512

Browse files
authored
fix(db-mongodb): treat empty strings as null / undefined for exists queries (#8337)
v2 PR [here](#8336)
1 parent 81a972d commit e916512

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

packages/db-mongodb/src/queries/sanitizeQueryValue.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,32 @@ export const sanitizeQueryValue = ({
202202
$regex: formattedValue.replace(/[\\^$*+?.()|[\]{}]/g, '\\$&'),
203203
}
204204
}
205+
206+
if (operator === 'exists') {
207+
formattedValue = formattedValue === 'true' || formattedValue === true
208+
209+
if (formattedValue) {
210+
return {
211+
rawQuery: {
212+
$and: [
213+
{ [path]: { $exists: true } },
214+
{ [path]: { $ne: null } },
215+
{ [path]: { $ne: '' } },
216+
],
217+
},
218+
}
219+
} else {
220+
return {
221+
rawQuery: {
222+
$or: [
223+
{ [path]: { $exists: false } },
224+
{ [path]: { $eq: null } },
225+
{ [path]: { $eq: '' } }, // Treat empty string as null / undefined
226+
],
227+
},
228+
}
229+
}
230+
}
205231
}
206232

207233
if (

0 commit comments

Comments
 (0)