简体   繁体   English

如何在mongoDb的子文档中添加新的数组元素

[英]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.

相关问题 MongoDB:更新子文档中的特定数组元素 - MongoDB: Updating A specific array element in a sub document 如何在MongoDB上的文档中将数组的元素分配给新键 - How can I assign an element of the array to a new key in the document on MongoDB 使用MongoDB,当元素位置未知时,如何更新子数组的子文档? - Using MongoDB, how do I update a sub-document of a sub-array when element position is unknown? 当且仅当新文档中存在元素数据时更新数组元素 mongodb - Update element of an array if and only if data for element is present in new document mongodb 仅当mongo集合中子数组的大小小于50时,如何将新项目添加到特定文档的子数组 - How to add a new item to the sub array of a specific document only if the size of the sub array is less than 50 in mongo collection 如何将现有的子文档推送到 MongoDB 中的嵌入式数组中? - How to push an existing sub-document into an embedded array in MongoDB? 如何在 MongoDB 的嵌套数组中获取子文档? - How to get the sub document within a nested array in MongoDB? PHP MongoDB \\ Client更新一个将新字段添加到现有文档数组 - PHP MongoDB\Client updateOne add new field to existing document array mongodb:为每个文档的数组属性的每个 object 添加新属性 - mongodb : add new property to each object of an array property of every document 在MongoDB中查找并替换子子数组元素 - Find and replace a sub sub array element in MongoDB
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM