【问题标题】:mongojs notation with $set带有 $set 的 mongojs 表示法
【发布时间】:2014-01-25 20:17:09
【问题描述】:

这是更新 mongojs 对象的方式:

        db.books.update(
       { _id: tg._id },
        {
         $set: 
         { 
         comment: "Hello", 
         }, 
       },

这假设有一个名为“comment”的属性。 您也可以通过字符串执行此操作。所以 set 子句看起来像这样:

         $set: 
         { 
         'comment': "Hello", 
         }, 

我的意思是:我想创建一个动态属性,它可能是

                var type = "external_property" 

但是当我尝试添加属性类型时将被分配。 但是 type 应该被读取为一个字符串,提供 'external_comment'。

我怎样才能做到这一点?

【问题讨论】:

    标签: string mongojs


    【解决方案1】:

    试试这个:

    var dynamic_property = 'my_dynamic_property', // or whatever you want
        update = {
            $set: {}
        };
    
    update.$set[dynamic_property] = 'Hello';
    
    db.books.update( {_id:tg._id}, update, ... );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多