我有一个带有表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
}
通过此过滤器选择用户的最佳方法是什么?
答案 0 :(得分:1)
如果所有过滤器都与表格字段相对应,则只需使用where
:
UserModel.find({ where: filter });
但是由于您有minAge
和maxAge
,因此可以添加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]
}
}
});