Search code examples
arraysfilterwhere-clausecontainsloopback

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 !


Solution

  • 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
            }
        }
    }