0

I want to change the field value type from string to object.

...{value: "my title"}... ===> ...{value:{value:"my title ABC"}}

In the Mongo shell, I present this field through the JavaScript var articlePath. Now, how can I use this var to update the field value?

The following code is create a new field articlePath instead of using the var's value.

var articlePath = "layout.content.header." + i + "." + j + ".value"
var articleValue = block.value + " ABC"
db.mycollection.update(
    {_id: catId},
    {
        $set: {
            articlePath: {value: articleValue}
        }
    },
    function(err, numberUpdated) {
        print("--->err: ", err)
    }
)
Tomalak
  • 332,285
  • 67
  • 532
  • 628

2 Answers2

0

Try out following snippet

var articlePath = {};
var keyName = "layout.content.header." + i + "." + j + ".value";
var articleValue = block.value + " ABC";
articlePath[keyName] = articleValue;

db.mycollection.update(
    {_id: catId},
    {
        $set: articlePath
    },
    function(err, numberUpdated) {
        print("--->err: ", err)
    }
)

Thanks

Dineshaws
  • 2,065
  • 16
  • 26
0

You can't use literal notation to set the name of a key via the value of a variable. Try this way:

var update = { "$set" : { } }
update["$set"][articlePath] = { "value" : articleValue }
db.mycollection.update({ "_id" : catId }, update, *callback*)
wdberkeley
  • 11,531
  • 1
  • 28
  • 23