How to query models by a property that is an array

泪湿孤枕 提交于 2019-12-05 06:16:23

问题


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.


回答1:


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




回答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



来源:https://stackoverflow.com/questions/26515167/how-to-query-models-by-a-property-that-is-an-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!