File tree Expand file tree Collapse file tree 2 files changed +33
-5
lines changed
packages/db-mongodb/src/queries Expand file tree Collapse file tree 2 files changed +33
-5
lines changed Original file line number Diff line number Diff line change @@ -193,17 +193,19 @@ export async function buildSearchParam({
193
193
194
194
if ( field . type === 'relationship' || field . type === 'upload' ) {
195
195
let hasNumberIDRelation
196
+ let multiIDCondition = '$or'
197
+ if ( operatorKey === '$ne' ) multiIDCondition = '$and'
196
198
197
199
const result = {
198
200
value : {
199
- $or : [ { [ path ] : { [ operatorKey ] : formattedValue } } ] ,
201
+ [ multiIDCondition ] : [ { [ path ] : { [ operatorKey ] : formattedValue } } ] ,
200
202
} ,
201
203
}
202
204
203
205
if ( typeof formattedValue === 'string' ) {
204
206
if ( mongoose . Types . ObjectId . isValid ( formattedValue ) ) {
205
- result . value . $or . push ( {
206
- [ path ] : { [ operatorKey ] : new ObjectId ( formattedValue ) } ,
207
+ result . value [ multiIDCondition ] . push ( {
208
+ [ path ] : { [ operatorKey ] : ObjectId ( formattedValue ) } ,
207
209
} )
208
210
} else {
209
211
; ( Array . isArray ( field . relationTo ) ? field . relationTo : [ field . relationTo ] ) . forEach (
@@ -218,11 +220,13 @@ export async function buildSearchParam({
218
220
)
219
221
220
222
if ( hasNumberIDRelation )
221
- result . value . $or . push ( { [ path ] : { [ operatorKey ] : parseFloat ( formattedValue ) } } )
223
+ result . value [ multiIDCondition ] . push ( {
224
+ [ path ] : { [ operatorKey ] : parseFloat ( formattedValue ) } ,
225
+ } )
222
226
}
223
227
}
224
228
225
- if ( result . value . $or . length > 1 ) {
229
+ if ( result . value [ multiIDCondition ] . length > 1 ) {
226
230
return result
227
231
}
228
232
}
Original file line number Diff line number Diff line change @@ -618,6 +618,30 @@ describe('collections-rest', () => {
618
618
} )
619
619
} )
620
620
621
+ it ( 'should query relationships by not_equals' , async ( ) => {
622
+ const ogPost = await createPost ( {
623
+ relationMultiRelationTo : { relationTo : relationSlug , value : relation . id } ,
624
+ } )
625
+ await createPost ( )
626
+
627
+ const response = await restClient . GET ( `/${ slug } ` , {
628
+ query : {
629
+ where : {
630
+ and : [
631
+ {
632
+ 'relationMultiRelationTo.value' : { not_equals : relation . id } ,
633
+ } ,
634
+ ] ,
635
+ } ,
636
+ } ,
637
+ } )
638
+ const result = await response . json ( )
639
+
640
+ expect ( response . status ) . toEqual ( 200 )
641
+ const foundExcludedDoc = result . docs . some ( ( doc ) => ogPost . id === doc . id )
642
+ expect ( foundExcludedDoc ) . toBe ( false )
643
+ } )
644
+
621
645
describe ( 'relationTo multi hasMany' , ( ) => {
622
646
it ( 'nested by id' , async ( ) => {
623
647
const post1 = await createPost ( {
You can’t perform that action at this time.
0 commit comments