2016-03-05 32 views
1
Specimen.find({ filter_fin : filter_spec }, 
    { 
     // Get only the taxonomy stuff 
     "taxonomy.phylum": 1, 
     "taxonomy.class": 1, 
     "taxonomy.order": 1, 
     "taxonomy.family": 1, 
     "taxonomy.genus": 1, 
     "taxonomy.species": 1, 
     "common_name": 1, 
     "last_edit": 1 
    }, 

以上是我的代碼,我的問題是,Specimen.find({「taxonomy.class」:filter_spec},它的工作原理。但是當把文本(「taxonomy.class」如果我不能做這個工作,將是非常低效的,因爲我必須使它處理幾個案例。MEAN STACK變量比較

+0

你是如何調用它,當你使用變量,您可以編輯您的問題,包括一部分? – chridam

+0

@chridam Specimen.find({filter_fin:filter_spec}, 這正是我如何嘗試使用它與變量(filter_fin) – Paul

+0

你可以請編輯您的問題,而不是在這個額外的信息的意見? – chridam

回答

0

如果你想把對象在變量屬性,那麼你需要使用square bracket notation構造域對象如下:

var query = { }, 
    projection = { 
     // Get only the taxonomy stuff 
     "taxonomy.phylum": 1, 
     "taxonomy.class": 1, 
     "taxonomy.order": 1, 
     "taxonomy.family": 1, 
     "taxonomy.genus": 1, 
     "taxonomy.species": 1, 
     "common_name": 1, 
     "last_edit": 1 
    }; 

query[filter_fin] = filter_spec; 
Specimen.find(query, projection, callback); 

或者使用computed property names (ES6)

var query = { 
     [filter_fin]: filter_spec 
    }, 
    projection = { 
     // Get only the taxonomy stuff 
     "taxonomy.phylum": 1, 
     "taxonomy.class": 1, 
     "taxonomy.order": 1, 
     "taxonomy.family": 1, 
     "taxonomy.genus": 1, 
     "taxonomy.species": 1, 
     "common_name": 1, 
     "last_edit": 1 
    }; 

Specimen.find(query, projection, callback);