fix(db-mongodb): treat empty strings as null / undefined for exists queries (#8337)

v2 PR [here](https://github.com/payloadcms/payload/pull/8336)
This commit is contained in:
Patrik
2024-09-20 15:27:07 -04:00
committed by GitHub
parent 81a972d966
commit e916512fa7

View File

@@ -202,6 +202,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 (