From 90769267acce461f547a8879f63ad3b93ed81470 Mon Sep 17 00:00:00 2001 From: Sasha <64744993+r1tsuu@users.noreply.github.com> Date: Mon, 28 Oct 2024 22:55:15 +0200 Subject: [PATCH 1/2] add test --- test/joins/int.spec.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/test/joins/int.spec.ts b/test/joins/int.spec.ts index d700379dda8..69cde56d9db 100644 --- a/test/joins/int.spec.ts +++ b/test/joins/int.spec.ts @@ -617,6 +617,24 @@ describe('Joins Field', () => { expect(response.data.Category.relatedPosts.docs[0].title).toStrictEqual('test 3') }) }) + + it('should work id.in querying with joins', async () => { + const allCategories = await payload.find({ collection: 'categories', pagination: false }) + + const allCategoriesByIds = await restClient + .GET(`/categories`, { + query: { + where: { + id: { + in: allCategories.docs.map((each) => each.id), + }, + }, + }, + }) + .then((res) => res.json()) + + expect(allCategories.totalDocs).toBe(allCategoriesByIds.totalDocs) + }) }) async function createPost(overrides?: Partial, locale?: Config['locale']) { From 94d8b8b8c16573e7a3a93ffd524e104844fd35b1 Mon Sep 17 00:00:00 2001 From: Sasha <64744993+r1tsuu@users.noreply.github.com> Date: Mon, 28 Oct 2024 23:31:28 +0200 Subject: [PATCH 2/2] fix(db-mongodb): id in querying using a comma-delimited value --- packages/db-mongodb/src/queries/sanitizeQueryValue.ts | 6 +++++- test/joins/int.spec.ts | 4 ++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/packages/db-mongodb/src/queries/sanitizeQueryValue.ts b/packages/db-mongodb/src/queries/sanitizeQueryValue.ts index 8d0d1e4f349..037028969b1 100644 --- a/packages/db-mongodb/src/queries/sanitizeQueryValue.ts +++ b/packages/db-mongodb/src/queries/sanitizeQueryValue.ts @@ -78,7 +78,11 @@ export const sanitizeQueryValue = ({ return { operator: formattedOperator, val: undefined } } } - } else if (Array.isArray(val)) { + } else if (Array.isArray(val) || (typeof val === 'string' && val.split(',').length > 1)) { + if (typeof val === 'string') { + formattedValue = createArrayFromCommaDelineated(val) + } + formattedValue = formattedValue.reduce((formattedValues, inVal) => { const newValues = [inVal] if (!hasCustomID) { diff --git a/test/joins/int.spec.ts b/test/joins/int.spec.ts index 69cde56d9db..9fa1cef6e69 100644 --- a/test/joins/int.spec.ts +++ b/test/joins/int.spec.ts @@ -618,7 +618,7 @@ describe('Joins Field', () => { }) }) - it('should work id.in querying with joins', async () => { + it('should work id.in command delimited querying with joins', async () => { const allCategories = await payload.find({ collection: 'categories', pagination: false }) const allCategoriesByIds = await restClient @@ -626,7 +626,7 @@ describe('Joins Field', () => { query: { where: { id: { - in: allCategories.docs.map((each) => each.id), + in: allCategories.docs.map((each) => each.id).join(','), }, }, },