diff --git a/packages/db-mongodb/src/queries/sanitizeQueryValue.ts b/packages/db-mongodb/src/queries/sanitizeQueryValue.ts index 3de2aa6cb..026555b9c 100644 --- a/packages/db-mongodb/src/queries/sanitizeQueryValue.ts +++ b/packages/db-mongodb/src/queries/sanitizeQueryValue.ts @@ -417,7 +417,7 @@ export const sanitizeQueryValue = ({ return buildExistsQuery( formattedValue, path, - !['relationship', 'upload'].includes(field.type), + !['checkbox', 'relationship', 'upload'].includes(field.type), ) } } diff --git a/test/fields/collections/Checkbox/index.ts b/test/fields/collections/Checkbox/index.ts index 0cf16ae66..b6157c230 100644 --- a/test/fields/collections/Checkbox/index.ts +++ b/test/fields/collections/Checkbox/index.ts @@ -10,6 +10,10 @@ const CheckboxFields: CollectionConfig = { type: 'checkbox', required: true, }, + { + name: 'checkboxNotRequired', + type: 'checkbox', + }, ], } diff --git a/test/fields/int.spec.ts b/test/fields/int.spec.ts index 890c88e22..3dca326c0 100644 --- a/test/fields/int.spec.ts +++ b/test/fields/int.spec.ts @@ -30,6 +30,7 @@ import { clearAndSeedEverything } from './seed.js' import { arrayFieldsSlug, blockFieldsSlug, + checkboxFieldsSlug, collapsibleFieldsSlug, groupFieldsSlug, relationshipFieldsSlug, @@ -1349,6 +1350,58 @@ describe('Fields', () => { }) }) + describe('checkbox', () => { + beforeEach(async () => { + await payload.delete({ + collection: checkboxFieldsSlug, + where: { + id: { + exists: true, + }, + }, + }) + }) + + it('should query checkbox fields with exists operator', async () => { + const existsTrueDoc = await payload.create({ + collection: checkboxFieldsSlug, + data: { + checkbox: true, + checkboxNotRequired: false, + }, + }) + + const existsFalseDoc = await payload.create({ + collection: checkboxFieldsSlug, + data: { + checkbox: true, + }, + }) + + const existsFalse = await payload.find({ + collection: checkboxFieldsSlug, + where: { + checkboxNotRequired: { + exists: false, + }, + }, + }) + expect(existsFalse.totalDocs).toBe(1) + expect(existsFalse.docs[0]?.id).toEqual(existsFalseDoc.id) + + const existsTrue = await payload.find({ + collection: checkboxFieldsSlug, + where: { + checkboxNotRequired: { + exists: true, + }, + }, + }) + expect(existsTrue.totalDocs).toBe(1) + expect(existsTrue.docs[0]?.id).toEqual(existsTrueDoc.id) + }) + }) + describe('unique indexes', () => { it('should throw validation error saving on unique fields', async () => { const data = { diff --git a/test/fields/payload-types.ts b/test/fields/payload-types.ts index f42ac4539..3dfa9f9d8 100644 --- a/test/fields/payload-types.ts +++ b/test/fields/payload-types.ts @@ -730,6 +730,7 @@ export interface TextField { export interface CheckboxField { id: string; checkbox: boolean; + checkboxNotRequired?: boolean | null; updatedAt: string; createdAt: string; } @@ -2318,6 +2319,7 @@ export interface LocalizedTabsBlockSelect { */ export interface CheckboxFieldsSelect { checkbox?: T; + checkboxNotRequired?: T; updatedAt?: T; createdAt?: T; }