我使用下面的查询:如果字段不存在或为空,则忽略查询中的字段?
$and : [
{$where : 'function(){if(this.vehicle.class){return this.vehicle.class == "Car";};return true;}'},
{$where : 'function(){if(this.vehicle.make){return this.vehicle.make == "MERCEDES-BENZ";};return true;}'},
{$where : 'function(){if(this.vehicle.model){return this.vehicle.model == "320";};return true;}'},
{$where : 'function(){if(this.price && this.price.min){return this.price.min >= 1000;};return true;}'},
{$where : 'function(){if(this.price && this.price.max){return this.price.max <= 1000;};return true;}'}
]
是忽略字段查询如果不设置字段IST更优雅的方式或为空?使用本地mongo查询运算符?
“vehicle.model”:{$存在:真,$类型:2,$于:[ “320”]} 将现场如果键完全不存在被忽略? – dima 2014-09-21 14:50:30
伟大的答案,但它有antoher效果不像我上面写在问题中的代码。如果该字段不存在,则根本不能在查询条件中使用它。 – dima 2014-09-21 14:51:48
@dima - 修改了我的答案,使其更具体。 – BatScream 2014-09-21 16:59:42