[英]How to control multiple update in one collection field in mongo and javascript?
[英]How to use the javascript var to reference the Mongo DB collection's field
我想将字段value
类型从字符串更改为对象。
...{value: "my title"}... ===> ...{value:{value:"my title ABC"}}
在Mongo Shell中,我通过JavaScript var articlePath
展示了此字段。 现在,如何使用此var更新字段值?
下面的代码创建一个新的字段articlePath
而不使用var的值。
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)
}
)
尝试以下片段
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)
}
)
谢谢
您不能使用文字表示法通过变量的值设置键的名称。 尝试这种方式:
var update = { "$set" : { } }
update["$set"][articlePath] = { "value" : articleValue }
db.mycollection.update({ "_id" : catId }, update, *callback*)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.