Javascript 如何在mongo数据库中从字符串传递动态键值
我尝试动态访问mongo数据库中的字段 目标字段由以下行给出:Javascript 如何在mongo数据库中从字符串传递动态键值,javascript,mongodb,meteor,Javascript,Mongodb,Meteor,我尝试动态访问mongo数据库中的字段 目标字段由以下行给出: var contextTarget= Session.get('contextLayout')+"BackgroundColor"; 在哪里 Session.get('contextLayout')可以是一个字符串,也可以是一个集合的id(例如userHomeBackgroundColor或56xhPYg8w3Qtv9xPLBackgroundColor) 我不知道如何访问该值。 我尝试了以下代码: var conte
var contextTarget= Session.get('contextLayout')+"BackgroundColor";
在哪里
Session.get('contextLayout')
可以是一个字符串,也可以是一个集合的id(例如userHomeBackgroundColor或56xhPYg8w3Qtv9xPLBackgroundColor)
我不知道如何访问该值。
我尝试了以下代码:
var contextTarget= Session.get('contextLayout')+"BackgroundColor";
var tempStore ={};
tempStore['target']=contextTarget;
var newTarget = tempStore.target;
console.log(newTarget);//return the correct value
var color = customConfiguration.findOne({'author':auth}).newTarget;
console.log(color);//return undefined
它不起作用。我想这是因为newTarget变量是一个字符串,因为如果我直接在控制台中写入预期结果,它就会工作。我该怎么做
编辑。
就像肯尼说的:
var color = customConfiguration.findOne({'author':auth})[contextTarget];
事实上,没有必要经过对象tempStore。方括号中的键正确发送。我已经试过了,但在方括号前有一个点。
@Michael:对于模式,我没有这个集合的模式,因为键名是动态创建的。
非常感谢。如果我理解正确,最后,您希望获得
customConfiguration
对象的变量属性,其名称包含在newTarget
中
或许可以试试这个:
var color = customConfiguration.findOne({'author':auth})[newTarget];
基本上,当您键入时:
someObject.toto = 'someValue';
var someAttribute = 'toto';
console.log(someObject.someAttribute); //undefined
console.log(someObject[someAttribute]); // 'someValue'
当使用点符号调用时,JavaScript总是假定someAttribute
是someObject
的属性。不是作用域中的变量集。如果需要字符串变量中包含的属性toto
(在我的示例中为someAttribute
)的值,则必须将此变量作为索引传递,如下所示:
console.log(someObject[someAttribute]); // 'someValue'
[
{"userHomeBackgroundColor": "red"},
{"56xhPYg8w3Qtv9xPLBackgroundColor": "green"},
{"someOtherConfigProperty" : "someOtherValue"}
]
var selector = {}; // Empty selector object for findOne
selector[contextTarget] = // Add dynamic property and as its value
{$exists: true}; // an object with an $exists-property
// with value true
// now find the (first) configuration document having the dynamic property
var configDocument = customConfiguration.findOne(selector);
// read the value of the dynamic property of the configuration document
var color = configDocument[contextTarget];
如果我理解正确,最后,您希望获得
customConfiguration
对象的变量属性,该属性的名称包含在newTarget
中
或许可以试试这个:
var color = customConfiguration.findOne({'author':auth})[newTarget];
基本上,当您键入时:
someObject.toto = 'someValue';
var someAttribute = 'toto';
console.log(someObject.someAttribute); //undefined
console.log(someObject[someAttribute]); // 'someValue'
当使用点符号调用时,JavaScript总是假定someAttribute
是someObject
的属性。不是作用域中的变量集。如果需要字符串变量中包含的属性toto
(在我的示例中为someAttribute
)的值,则必须将此变量作为索引传递,如下所示:
console.log(someObject[someAttribute]); // 'someValue'
[
{"userHomeBackgroundColor": "red"},
{"56xhPYg8w3Qtv9xPLBackgroundColor": "green"},
{"someOtherConfigProperty" : "someOtherValue"}
]
var selector = {}; // Empty selector object for findOne
selector[contextTarget] = // Add dynamic property and as its value
{$exists: true}; // an object with an $exists-property
// with value true
// now find the (first) configuration document having the dynamic property
var configDocument = customConfiguration.findOne(selector);
// read the value of the dynamic property of the configuration document
var color = configDocument[contextTarget];
如果我理解正确,最后,您希望获得
customConfiguration
对象的变量属性,该属性的名称包含在newTarget
中
或许可以试试这个:
var color = customConfiguration.findOne({'author':auth})[newTarget];
基本上,当您键入时:
someObject.toto = 'someValue';
var someAttribute = 'toto';
console.log(someObject.someAttribute); //undefined
console.log(someObject[someAttribute]); // 'someValue'
当使用点符号调用时,JavaScript总是假定someAttribute
是someObject
的属性。不是作用域中的变量集。如果需要字符串变量中包含的属性toto
(在我的示例中为someAttribute
)的值,则必须将此变量作为索引传递,如下所示:
console.log(someObject[someAttribute]); // 'someValue'
[
{"userHomeBackgroundColor": "red"},
{"56xhPYg8w3Qtv9xPLBackgroundColor": "green"},
{"someOtherConfigProperty" : "someOtherValue"}
]
var selector = {}; // Empty selector object for findOne
selector[contextTarget] = // Add dynamic property and as its value
{$exists: true}; // an object with an $exists-property
// with value true
// now find the (first) configuration document having the dynamic property
var configDocument = customConfiguration.findOne(selector);
// read the value of the dynamic property of the configuration document
var color = configDocument[contextTarget];
如果我理解正确,最后,您希望获得
customConfiguration
对象的变量属性,该属性的名称包含在newTarget
中
或许可以试试这个:
var color = customConfiguration.findOne({'author':auth})[newTarget];
基本上,当您键入时:
someObject.toto = 'someValue';
var someAttribute = 'toto';
console.log(someObject.someAttribute); //undefined
console.log(someObject[someAttribute]); // 'someValue'
当使用点符号调用时,JavaScript总是假定someAttribute
是someObject
的属性。不是作用域中的变量集。如果需要字符串变量中包含的属性toto
(在我的示例中为someAttribute
)的值,则必须将此变量作为索引传递,如下所示:
console.log(someObject[someAttribute]); // 'someValue'
[
{"userHomeBackgroundColor": "red"},
{"56xhPYg8w3Qtv9xPLBackgroundColor": "green"},
{"someOtherConfigProperty" : "someOtherValue"}
]
var selector = {}; // Empty selector object for findOne
selector[contextTarget] = // Add dynamic property and as its value
{$exists: true}; // an object with an $exists-property
// with value true
// now find the (first) configuration document having the dynamic property
var configDocument = customConfiguration.findOne(selector);
// read the value of the dynamic property of the configuration document
var color = configDocument[contextTarget];
如果您可以描述集合的模式
customConfiguration
,这将非常有用。我猜它包含一个文档集合,每个文档只有一个属性(例如userHomeBackgroundColor
或56xhPYg8w3Qtv9xPLBackgroundColor
),您对一个具有正确属性名的文档感兴趣,以读取属性值。所以我猜,在JSON符号中,集合看起来像这样:
console.log(someObject[someAttribute]); // 'someValue'
[
{"userHomeBackgroundColor": "red"},
{"56xhPYg8w3Qtv9xPLBackgroundColor": "green"},
{"someOtherConfigProperty" : "someOtherValue"}
]
var selector = {}; // Empty selector object for findOne
selector[contextTarget] = // Add dynamic property and as its value
{$exists: true}; // an object with an $exists-property
// with value true
// now find the (first) configuration document having the dynamic property
var configDocument = customConfiguration.findOne(selector);
// read the value of the dynamic property of the configuration document
var color = configDocument[contextTarget];
如果这个假设是正确的,我将替换您原来的行
var color = customConfiguration.findOne({'author':auth}).newTarget;
比如说:
console.log(someObject[someAttribute]); // 'someValue'
[
{"userHomeBackgroundColor": "red"},
{"56xhPYg8w3Qtv9xPLBackgroundColor": "green"},
{"someOtherConfigProperty" : "someOtherValue"}
]
var selector = {}; // Empty selector object for findOne
selector[contextTarget] = // Add dynamic property and as its value
{$exists: true}; // an object with an $exists-property
// with value true
// now find the (first) configuration document having the dynamic property
var configDocument = customConfiguration.findOne(selector);
// read the value of the dynamic property of the configuration document
var color = configDocument[contextTarget];
但是为了验证这一点,我需要更多关于集合customConfiguration
模式的背景知识。您可能还需要检查文档中$exists
()和findOne()
()的文档
但是,如果集合customConfiguration
包含每个作者的一个配置文档,请将行更改为:
var color = customConfiguration.findOne({'author':auth})[newTarget];
此解决方案已在前面的回答中描述。如果您可以描述集合的架构
customConfiguration
,将非常有用。我猜它包含一个文档集合,每个文档只有一个属性(例如userHomeBackgroundColor
或56xhPYg8w3Qtv9xPLBackgroundColor
),您对一个具有正确属性名的文档感兴趣,以读取属性值。所以我猜,在JSON符号中,集合看起来像这样:
console.log(someObject[someAttribute]); // 'someValue'
[
{"userHomeBackgroundColor": "red"},
{"56xhPYg8w3Qtv9xPLBackgroundColor": "green"},
{"someOtherConfigProperty" : "someOtherValue"}
]
var selector = {}; // Empty selector object for findOne
selector[contextTarget] = // Add dynamic property and as its value
{$exists: true}; // an object with an $exists-property
// with value true
// now find the (first) configuration document having the dynamic property
var configDocument = customConfiguration.findOne(selector);
// read the value of the dynamic property of the configuration document
var color = configDocument[contextTarget];
如果这个假设是正确的,我将替换您原来的行
var color = customConfiguration.findOne({'author':auth}).newTarget;
比如说:
console.log(someObject[someAttribute]); // 'someValue'
[
{"userHomeBackgroundColor": "red"},
{"56xhPYg8w3Qtv9xPLBackgroundColor": "green"},
{"someOtherConfigProperty" : "someOtherValue"}
]
var selector = {}; // Empty selector object for findOne
selector[contextTarget] = // Add dynamic property and as its value
{$exists: true}; // an object with an $exists-property
// with value true
// now find the (first) configuration document having the dynamic property
var configDocument = customConfiguration.findOne(selector);
// read the value of the dynamic property of the configuration document
var color = configDocument[contextTarget];
但是为了验证这一点,我需要更多关于集合customConfiguration
模式的背景知识。您可能还需要检查文档中$exists
()和findOne()
()的文档
但是,如果集合customConfiguration
包含每个作者的一个配置文档,请将行更改为:
var color = customConfiguration.findOne({'author':auth})[newTarget];
此解决方案已在前面的回答中描述。如果您可以描述集合的架构
customConfiguration
,将非常有用。我猜它包含一个文档集合,每个文档只有一个属性(例如userHomeBackgroundColor
或56xhPYg8w3Qtv9xPLBackgroundColor
),您对一个具有正确属性名的文档感兴趣,以读取属性值。所以我猜,在JSON符号中,集合看起来像这样:
console.log(someObject[someAttribute]); // 'someValue'
[
{"userHomeBackgroundColor": "red"},
{"56xhPYg8w3Qtv9xPLBackgroundColor": "green"},
{"someOtherConfigProperty" : "someOtherValue"}
]
var selector = {}; // Empty selector object for findOne
selector[contextTarget] = // Add dynamic property and as its value
{$exists: true}; // an object with an $exists-property
// with value true
// now find the (first) configuration document having the dynamic property
var configDocument = customConfiguration.findOne(selector);
// read the value of the dynamic property of the configuration document
var color = configDocument[contextTarget];
如果这个假设是正确的,我会替换你的原版