我使用下面的查詢:如果字段不存在或爲空,則忽略查詢中的字段?
$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