【问题标题】:how to pass a dynamic key value from a string in mongo data base如何从mongo数据库中的字符串传递动态键值
【发布时间】:2015-04-30 23:20:28
【问题描述】:

我尝试动态访问 mongo 数据库中的字段。

目标字段由这一行给出:

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 变量是一个字符串,因为如果我直接在控制台中写出预期的结果,它就可以工作。我该怎么办?

编辑。

就像 BraveKenny 所说:

 var color = customConfiguration.findOne({'author':auth})[contextTarget];

事实上,没有必要通过对象 tempStore。使用方括号键可以正确发送。我已经尝试过了,但在方括号前加了一个点。 @Michael:对于模式,我没有这个集合的模式,因为键名是动态创建的。 非常感谢。

【问题讨论】:

    标签: javascript mongodb meteor


    【解决方案1】:

    如果我理解正确,最后,您想获取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'
    

    当使用点符号调用 someAttribute 时,JavaScript 将始终假定它是 someObject 的属性。不是在范围内设置的变量。如果您想要包含在字符串变量中的属性toto 的值(在我的情况下为someAttribute),则必须将此变量作为索引传递,如下所示:

    console.log(someObject[someAttribute]); // 'someValue'
    

    【讨论】:

    • console.log(someObject[someAttribute]);方括号对我有用。
    【解决方案2】:

    如果您能描述集合customConfiguration 的架构,将会很有帮助。我只是猜测它包含一组文档,每个文档只有一个属性(例如userHomeBackgroundColor 或56xhPYg8w3Qtv9xPLBackgroundColor),并且您对具有正确属性名称的一个文档感兴趣以读取属性值。所以我猜,在 JSON 表示法中,该集合看起来像这样:

    [
      {"userHomeBackgroundColor": "red"},
      {"56xhPYg8w3Qtv9xPLBackgroundColor": "green"},
      {"someOtherConfigProperty" : "someOtherValue"}
    ]
    

    如果这个假设是正确的,我会替换你原来的那行

    var color = customConfiguration.findOne({'author':auth}).newTarget;
    

    这样的:

    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 (http://docs.mongodb.org/manual/reference/operator/query/exists/#op._S_exists) 和findOne() (http://docs.mongodb.org/manual/reference/method/db.collection.findOne/) 的文档。

    但是,如果集合 customConfiguration 包含每个作者的一个配置文档,请将您的行更改为:

    var color = customConfiguration.findOne({'author':auth})[newTarget];
    

    此解决方案已在之前的答案中进行了描述。

    【讨论】:

    • var color = customConfiguration.findOne({'author':auth})[contextTarget];对我来说是正确的解决方案。我无法为此集合创建架构,因为键是动态创建的(例如,当作者发布新页面并选择颜色时)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-15
    • 2013-08-12
    • 1970-01-01
    • 2013-07-09
    相关资源
    最近更新 更多