在每个集合文档中添加新的非现有列

时间:2018-02-05 08:19:04

标签: mongodb-query

我在现有文档中添加新列 我的现有文件格式为:

{
      "_id": "5a6026e29cbcdc48083dae1a",
      "data": {
        "formList": [
          {
            "IdentificationDetails": [
              {
                "Status": "Data Entry Completed",
                "Form_Id": 1,
                "Interviewer_Name": "NAGARAJ.P",
                "Group_Id": 9,
                "Study_Id": "1",
                "Study_Name": "INDIAB",
                "Created_Emp_Id": "1",
                "Form_Name": "Food Frequency Questionnarie",
                "Visit_No": "1",
                "Volunteer_Id": "R270116",
                "Interview_Date": "2009-10-10T00:00:00Z",
                "Volunteer_Name": "RAJENDRA PAWAR"
              }
            ]
          },
          {
            "VFoodDetails": [
              {
                "Portion_Size": 15,
                "Serving_Size": "0",
                "Food_Id": 737,
                "Value": "0",
                "Portion_Tool": 18,
                "Volunteer_Id": "R270116",
                "Meal_Session": 121,
                "Frequency_Serving": "Never"
              },
              {
                "Portion_Size": 168,
                "Serving_Size": "0",
                "Food_Id": 740,
                "Value": "0",
                "Portion_Tool": 177,
                "Volunteer_Id": "R270116",
                "Meal_Session": 121,
                "Frequency_Serving": "Never"
              }
            ]
          }
        ]
      }
    }   

我需要将新列Group_Description添加到' IdentificationDetails'的对象中。阵列。

My Required Json Document is:

{
      "_id": "5a6026e29cbcdc48083dae1a",
      "data": {
        "formList": [
          {
            "IdentificationDetails": [
              {
                "Status": "Data Entry Completed",
                "Form_Id": 1,
                "Interviewer_Name": "NAGARAJ.P",
                "Group_Id": 9,
                "Study_Id": "1",
                "Study_Name": "INDIAB",
                "Created_Emp_Id": "1",
                "Form_Name": "Food Frequency Questionnarie",
                "Visit_No": "1",
                "Volunteer_Id": "R270116",
                "Interview_Date": "2009-10-10T00:00:00Z",
                "Volunteer_Name": "RAJENDRA PAWAR",
              }
            ]
          },
          {
            "VFoodDetails": [
              {
                "Portion_Size": 15,
                "Serving_Size": "0",
                "Food_Id": 737,
                "Value": "0",
                "Portion_Tool": 18,
                "Volunteer_Id": "R270116",
                "Meal_Session": 121,
                "Frequency_Serving": "Never"
              },
              {
                "Portion_Size": 168,
                "Serving_Size": "0",
                "Food_Id": 740,
                "Value": "0",
                "Portion_Tool": 177,
                "Volunteer_Id": "R270116",
                "Meal_Session": 121,
                "Frequency_Serving": "Never",
                 "Group_Description": "Urban"
              }
            ]
          }
        ]
      }
    }

我的更新命令,它创建了json的最后一列:

  

db.FrequencyQuestionForm.update({" data.formList.IdentificationDetails.Group_Id"         :9.0},{$ set:{" Group_Description":" Urban"}},{multi:true})

{
      "_id": "5a6026e29cbcdc48083dae1a",
      "data": {
        "formList": [
          {
            "IdentificationDetails": [
              {
                "Status": "Data Entry Completed",
                "Form_Id": 1,
                "Interviewer_Name": "NAGARAJ.P",
                "Group_Id": 9,
                "Study_Id": "1",
                "Study_Name": "INDIAB",
                "Created_Emp_Id": "1",
                "Form_Name": "Food Frequency Questionnarie",
                "Visit_No": "1",
                "Volunteer_Id": "R270116",
                "Interview_Date": "2009-10-10T00:00:00Z",
                "Volunteer_Name": "RAJENDRA PAWAR",
                "Group_Description": "Urban"
              }
            ]
          },
          {
            "VFoodDetails": [
              {
                "Portion_Size": 15,
                "Serving_Size": "0",
                "Food_Id": 737,
                "Value": "0",
                "Portion_Tool": 18,
                "Volunteer_Id": "R270116",
                "Meal_Session": 121,
                "Frequency_Serving": "Never"
              },
              {
                "Portion_Size": 168,
                "Serving_Size": "0",
                "Food_Id": 740,
                "Value": "0",
                "Portion_Tool": 177,
                "Volunteer_Id": "R270116",
                "Meal_Session": 121,
                "Frequency_Serving": "Never"
              }
             ]
          }
        ]
      }, 
      "Group_Description": "Urban"
    }

1 个答案:

答案 0 :(得分:1)

在集合中的所有mongodb文档中添加新的不存在列的命令是:

exists