2016-03-05 93 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);