How to query models by array property

I am trying to perform a "findOne" operation on a model that has an array property and filters the results to only display a list if the im search string is in that array.

Example:

  var AppUser = server.loopback.getModel('AppUser');
  AppUser.create({
    "name":"juan"
    "favoriteLetters":["a","b","c"]
  },function(){
    AppUser.findOne({where:{favoriteLetters:'a'}},function(error,appUser){
      console.log(error,appUser);
    });
  });

      

So, in this case, I want to find the "appUser" that has the favorite letter "a".

Thank.

+3


source to share


2 answers


As far as I understand, the possibility of this type of query depends on the underlying data source and is not yet supported for relational databases. But should be fine with memory or mongodb. More details and syntax for the request is here: https://groups.google.com/d/msg/loopbackjs/8c8kw8EMiPU/yev3lsmrTFUJ



+2


source


For everyone who lands here, this query in your model is correct (for Mongo anyway).

{where:{favoriteLetters:'a'}



Link:

Find a document with an array that contains a specific value

+2


source







All Articles