Mongodb: updating a variable field name

mongodb, node.js

Solution

You can use an intermediary object:

var update = { $set : {} };
update.$set['hello.' + newFieldName] = 'Amazing Grace';
db.bios.update({ _id : 3 }, update, ...)

Problem

I want to add a new field with a variable name to an object in the DB : meaning, I don't know the name of the field, but it's held in a variable "newFieldName". So what I want to do is basically this: ``` var newFieldName = "world"; db.bios.update( { _id: 3 }, { $set: { "hello."+newFieldName: "Amazing Grace" } } ) ``` After the update, I expect the object "hello" to have a field "world" with the value "Amazing Grace". but this doesn't even compile, let alone work. How can I do it?

Original source