Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loopback where filter on an array type field

I'm working on a loopback project, I have a model called Depot which has a field likersList, that fiels is an array of string (user id)
I want to find all Depots which has not a specific userId in it's likersList , I tried that filter, but it doesn't work

let userId=req.accessToken.userId;
let filter={
   where:{
       and:[
           ....
           {likersList:{nin:[userId]}}
       ]
   }
   ....
}

I also try that {likerList:{neq:userId}} , doesn't work too
But if try this likersList:userId that returns all Depots which has userIdin it's likersList , so I expected that doing {likerList:{neq:userId}} sould do a opposite

All I want is to know how to check if a field (array type) contains a certain value

Help please !

like image 813
Mahamadou DOUMBIA Avatar asked Sep 06 '25 03:09

Mahamadou DOUMBIA


2 Answers

It works here:

It brings all objects that dont contains userId inside the likersList.

let filter={
    where: {
        {
            likersList: {neq: userId}
        }
    }
}

It brings only objects that contains userId inside the likersList.

let filter={
    where: {
        {
            likersList: userId
        }
    }
}
like image 198
Fanuel Nunes Avatar answered Sep 11 '25 02:09

Fanuel Nunes


What database are you using for your loopback application? I have seen scenarios where nin and inq filter doesn't work for Oracle database. I have used appropriate regular expression filter as a workaround.

like image 27
abskmj Avatar answered Sep 11 '25 00:09

abskmj