CNode

mongoose条件查询时如何实现判断

问答
ZZhangHang-z发布于11 年前最后回复11 年前2 回复5152 浏览0 收藏
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)

L
LeafInStrom#1·11 年前
	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){
			
		})
I
i5ting#2·11 年前

有or和and足够了

参与回复
登录后即可参与回复。登录