for your request
COLLECTIONNAME.update( { _id: this._id },{ $set: { VARIABLE1 : VARIABLE2 } } ); mongodb will take the value VARIABLE2, but it will treat "VARIABLE1" as a field in the document.
To pass a variable whose value is the name of the field:
you will need to create a javascript object and use it like this:
var obj={}; obj[VARIABLE1] = VARIABLE2;
If you want to have multiple fields this way, just add them to the same object:
obj[VARIABLE3]= VARIABLE4;
Then perform the update operation as:
db.collectionname.update( { _id: this._id },{ $set: obj } );
source share