我有一个使用MongoDB的Node.js应用程序
我的客户集合模式如下:
{
'_id' : 1234341264876876143 ,
'profile':{
'name': 'bob',
'email': 'bob@example.com',
'DOB': '13th April 1976'
}
}
我想从我的Node应用程序中找到特定客户的
profile.email
字段,仅限此字段。var field_name = "email"; // field_name selected programmatically from an array
db.collection('customers').find(
{'_id':8965698756579084} ,
{'profile.' + field_name :true} // expecting it to become 'profile.email'
)
但是,它会出现以下错误:
{'profile.' + field_name :true}
^
SyntaxError: Unexpected token +
at Module._compile (module.js:437:25)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:362:17)
at require (module.js:378:17)
我该如何动态生成字符串以检索我想要获取的具体字段?
var profileFieldName='profile.'+field_name;
- WiredPrairie