0

This is how a mongojs objects gets updated:

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

This assumes that there is a property called 'comment'. You can do this also via string. So the set clause looks like this:

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

My point is: I want to create a dnymaic property, which might be

                var type = "external_property" 

but when I try to add the property type will be assigned. But type should be read as a string, providing 'external_comment'.

How can I achieve that?

1 Answers1

0

Try this:

var dynamic_property = 'my_dynamic_property', // or whatever you want
    update = {
        $set: {}
    };

update.$set[dynamic_property] = 'Hello';

db.books.update( {_id:tg._id}, update, ... );
heinob
  • 19,127
  • 5
  • 41
  • 61