如何查询具有属性的对象包含loopback中的值

时间:2016-05-22 08:00:08

标签: loopbackjs strongloop

我有一个模型event,其中引用了许多user

"relations": {
  "attendees": {
    "type": "referencesMany",
    "model": "user",
    "foreignKey": "attendeeIds",
    "options": {
      "validate": true,
      "forceId": false,
      "persistent": true
   }
}

如何查询哪个attendees属性包含给定值的事件? attendees是一个包含userId的数组。

例如:

Event.find({
    where: {
      attendees: {
        contains: givenUserId
      }
    }
  }
)

1 个答案:

答案 0 :(得分:0)

此功能称为'在第2级属性上过滤' ,引用为here,并且正在为内存和MongoDB连接器实施。它仍有一些问题。 对于SQL连接器,这还没有得到支持 - 正如在这个公开讨论链接中所提到的那样 - 它需要一些现在无法提供的时间。

围绕here使用 regexp 可能适用于基础JSON字符串并直接通过 datasource.connector.execute()

执行SQL

简单的实施可能是:

AnyModel.getApp((err, app) => {
  if (err) {
    console.log(err);
    next(err);
  }

  // Using MySQL
  const MySQL = app.dataSources.MySQL;

  MySQL.connector.execute(`select * from Table where column like '%${string}%'`,
    [], {}, (err, data) => {
      if (err) {
        console.log(err);
        next(err);
      }

      console.log(data);
      next();
    });
});