[英]How add new array element in sub document in mongoDb
This is my JSON 这是我的JSON
{
"_id" : 2313,
"project_id" : "313",
"project_task" : [
{
"switchName" : "new 2",
"slot" : "werwr",
"cdpDeviceId" : "weqqw",
"migrationEndDate" : "2015-11-24",
"migrationStartDate" : "2015-11-16",
"assigned_to" : "1319",
"task_id" : 1
},
{
"switchName" : "new 2",
"slot" : "werwr",
"cdpDeviceId" : "weqqw",
"migrationEndDate" : "2015-11-15",
"migrationStartDate" : "2015-11-24",
"assigned_to" : "",
"task_id" : 2
},
{
"switchName" : "new 2",
"slot" : "werwr",
"cdpDeviceId" : "weqqw",
"migrationEndDate" : "",
"migrationStartDate" : "",
"assigned_to" : "",
"task_id" : 3
}
]
}
In this array i need to add new element for sub document my expectation like this 在这个数组中,我需要为子文档添加新元素,我的期望是这样
{
"switchName" : "new 2",
"slot" : "werwr",
"cdpDeviceId" : "weqqw",
"migrationEndDate" : "2015-11-24",
"migrationStartDate" : "2015-11-16",
"assigned_to" : "1319",
"task_id" : 1,
"newKey" : "123"
}
db.getCollection('tableName').update({"_id" : ObjectId("2313")}, {$push: { "project_task.1.newKey": "123"}})
But I got this like, 但我有这样的感觉
{
"switchName" : "new 2",
"slot" : "werwr",
"cdpDeviceId" : "weqqw",
"migrationEndDate" : "2015-11-15",
"migrationStartDate" : "2015-11-24",
"assigned_to" : "",
"task_id" : 2,
"newKey" : [
"123"
]
}
Also is there any function to add same element for all of the array in a sub document rather than using PHP for loop. 还有任何功能可以为子文档中的所有数组添加相同的元素,而不是使用PHP进行循环。
Please help me out!! 请帮帮我!!
Yeah I found that simple solution 是的,我找到了简单的解决方案
using $set instead of $pull 使用$ set代替$ pull
The query would be like this, 查询将是这样,
db.getCollection('tableName').update({"_id" : ObjectId("2313")}, {$set: { "project_task.1.newKey": "123"}})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.