mongoose条件查询时如何实现判断
Model.find()
.where('type', one)
.where('published', two)
.where('nation', three)
.sort({'field': -1})
.exec(callback)
one, two, three 有可能是0, 代表搜索全部, 其值由前端传入, 但是数据库中每个字段由1开始, 我想要类似的效果
Model.find()
if (one != 0) {
.where('type', one)
}
if (two != 0) {
if (two != -1) {
.where('published', two)
} else {
.where('published').lte(2010)
}
}
if (three != 0) {
.where('nation', three)
}
.sort({'field': -1})
.exec(callback)
这样的结果在mongoodse该如何实现
2 回复
let option = {};
if (one != 0) {
option.type = one;
}
if (two != 0) {
if (two != -1) {
option.published = two;
} else {
option.published = {$lte: 2010};
}
}
if (three != 0) {
option.nation = three;
}
model.find(option)
.exec(function(err,items){
})
有or和and足够了