续集。如何找到很多参数

时间:2019-01-01 17:47:49

标签: javascript sequelize.js

我有一个带有表User (firstname, lastname, age, profilePicture, gender, intention)的postgre数据库,并且有一个对象过滤器:

filter = {
        firstname: String
        lastname: String,
        withPhoto: Boolean,
        minAge: Integer,
        maxAge: Integer,
        isMale: Boolean,
        intention: String //sex/friendship/communication
    }

通过此过滤器选择用户的最佳方法是什么?

1 个答案:

答案 0 :(得分:1)

如果所有过滤器都与表格字段相对应,则只需使用where

UserModel.find({ where: filter });

但是由于您有minAgemaxAge,因此可以添加between operator

UserModel.find({
  where: {
    firstname: filter.firstname
    lastname: filter.lastname,
    withPhoto: filter.withPhoto,
    isMale: filter.isMale,
    intention: filter.intention,
    age: {
      [Sequelize.Op.between]: [filter.minAge, filter.maxAge]
    }
  }
});