File tree Expand file tree Collapse file tree 2 files changed +48
-1
lines changed
packages/db-mongodb/src/queries Expand file tree Collapse file tree 2 files changed +48
-1
lines changed Original file line number Diff line number Diff line change @@ -81,7 +81,19 @@ export async function parseParams({
81
81
[ searchParam . path ] : searchParam . value ,
82
82
} )
83
83
} 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
+ }
85
97
}
86
98
} else if ( typeof searchParam ?. value === 'object' ) {
87
99
result = deepMergeWithCombinedArrays ( result , searchParam . value ?? { } , {
Original file line number Diff line number Diff line change @@ -336,6 +336,41 @@ describe('Relationships', () => {
336
336
expect ( query . docs ) . toHaveLength ( 1 ) // Due to limit: 1
337
337
} )
338
338
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
+
339
374
it ( 'should allow querying within blocks' , async ( ) => {
340
375
const rel = await payload . create ( {
341
376
collection : relationSlug ,
You can’t perform that action at this time.
0 commit comments