【发布时间】:2017-11-03 17:24:52
【问题描述】:
下面是 Mongodb 的 find 查询返回的 json 对象。我想在treatmentList 数组中添加一个新键。如何使用Nodejs 来实现?
我尝试过treatmentList[0].push({ new Key1: "new Value1"}),但它不起作用
实际和预期的输出如下 -
实际输出
[
{
"departmentImagepath": "",
"treatmentList": [
{
"shortDescription": "my shortdescription",
"healingTimeInDays": "8",
},
{
"shortDescription": "my new shortdescription",
"healingTimeInDays": "10",
}
]
}
]
预期输出
[
{
"departmentImagepath": "",
"treatmentList": [
{
"shortDescription": "my shortdescription",
"healingTimeInDays": "8",
"new Key1": "new value1" //New key to be added
},
{
"shortDescription": "my new shortdescription",
"healingTimeInDays": "10",
"new Key2": "new value2" //New key to be added
}
]
}
]
【问题讨论】:
-
arr[0]["treatmentList"][0]["new Key1"] = "new Value1" -
不知何故无法正常工作...
-
@derek
[mcve]也会这样做:minimal reproducible example
标签: javascript node.js mongodb