3

I'm trying to do a 'findOne' operation in a model that has an array property and filter the results to only list the item if the string im searching 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 a 'appUser' that has a favorite letter 'a'.

Thanks.

jpcapdevila
  • 207
  • 3
  • 8

2 Answers2

2

As far as I understood, possibility of such kind of a query depends on the underlying datasource and is not supported yet for relational DBs. But should be fine with memory storage or mongodb. More details and syntax for query is here: https://groups.google.com/d/msg/loopbackjs/8c8kw8EMiPU/yev3lsmrTFUJ

Alex V
  • 1,155
  • 8
  • 10
2

For anyone else who lands here, that query in your model is correct (for Mongo anyways).

{where:{favoriteLetters:'a'}

Reference:

Find document with array that contains a specific value

Community
  • 1
  • 1
abc123
  • 8,043
  • 7
  • 49
  • 80