Skip to content

Commit 857e984

Browse files
authored
fix(db-mongodb): querying relationships with where clause as an object with several conditions (#11953)
Fixes #11927 When trying to use the following notation: ```ts const { docs } = await payload.find({ collection: 'movies', depth: 0, where: { 'director.name': { equals: 'Director1' }, 'director.localized': { equals: 'Director1_Localized' }, }, }) ``` Currently, it respects only the latest condition and the first is ignored. However, this works fine: ```ts const { docs } = await payload.find({ collection: 'movies', depth: 0, where: { and: [ { 'director.name': { equals: 'Director1' }, }, { 'director.localized': { equals: 'Director1_Localized' }, }, ], }, }) ``` But this should be an equivalent to ``` where: { 'director.name': { equals: 'Director1' }, 'director.localized': { equals: 'Director1_Localized' }, }, ```
1 parent d47b753 commit 857e984

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,19 @@ export async function parseParams({
8181
[searchParam.path]: searchParam.value,
8282
})
8383
} else {
84-
result[searchParam.path] = searchParam.value
84+
if (result[searchParam.path]) {
85+
if (!result.$and) {
86+
result.$and = []
87+
}
88+
89+
result.$and.push({ [searchParam.path]: result[searchParam.path] })
90+
result.$and.push({
91+
[searchParam.path]: searchParam.value,
92+
})
93+
delete result[searchParam.path]
94+
} else {
95+
result[searchParam.path] = searchParam.value
96+
}
8597
}
8698
} else if (typeof searchParam?.value === 'object') {
8799
result = deepMergeWithCombinedArrays(result, searchParam.value ?? {}, {

test/relationships/int.spec.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,41 @@ describe('Relationships', () => {
336336
expect(query.docs).toHaveLength(1) // Due to limit: 1
337337
})
338338

339+
it('should allow querying by relationships with an object where as AND', async () => {
340+
const director = await payload.create({
341+
collection: 'directors',
342+
data: { name: 'Director1', localized: 'Director1_Localized' },
343+
})
344+
345+
const movie = await payload.create({
346+
collection: 'movies',
347+
data: { director: director.id },
348+
depth: 0,
349+
})
350+
351+
const { docs: trueRes } = await payload.find({
352+
collection: 'movies',
353+
depth: 0,
354+
where: {
355+
'director.name': { equals: 'Director1' },
356+
'director.localized': { equals: 'Director1_Localized' },
357+
},
358+
})
359+
360+
expect(trueRes).toStrictEqual([movie])
361+
362+
const { docs: falseRes } = await payload.find({
363+
collection: 'movies',
364+
depth: 0,
365+
where: {
366+
'director.name': { equals: 'Director1_Fake' },
367+
'director.localized': { equals: 'Director1_Localized' },
368+
},
369+
})
370+
371+
expect(falseRes).toStrictEqual([])
372+
})
373+
339374
it('should allow querying within blocks', async () => {
340375
const rel = await payload.create({
341376
collection: relationSlug,

0 commit comments

Comments
 (0)