How to and a new field and update value dynamically through mongo shell

I have address field value inside basicDetails,I want that value to bring to root field by adding a new field named address,so I am trying in this way but the issue is it is adding new field address in all the document but the value it is unable to update,I think I am wrong in syntax for referencing the value properly,Please help me .Thank in advance…

Updation Query:

db.businesslistings.updateMany({}, {$set: {“address”: ‘$attributes[0].basicDetails[1].valueString’}})

Result:

address: “$attributes[0].basicDetails[1].valueString”

Hello @Umasankar_Swain,

You need to use update with aggregation pipeline to use the value of the internal property.

Second, you need to share the actual document structure, and then I can suggest you the query and expected result.

Okay @turivishal ,so this is the structure of the document,

{
“attributes”: [
{
“basicDetails”: [
{
“id”: {
“$oid”: “”
},
“name”: “Name”,
“fieldType”: “textField”,
“fieldKey”: “nameField”,
“fieldDependendent”: “”,
“valueString”: “”,
“valueArray”: ,
“valueBoolean”: false
},
{
“id”: {
“$oid”: “”
},
“name”: “Address”,
“fieldType”: “textareaAddressField”,
“fieldKey”: “addressField”,
“fieldDependendent”: “”,
],
“valueString”: “address value”,
“valueArray”: ,
“valueBoolean”: false
},
],
“specificationDetails”:,
“additionalDetails”:
}
],
“status”: “”,
“address”:“$attributes[0].basicDetails[1].valueString”,
“createdAt”: ,
“updatedAt”: ,
}

Hello @Umasankar_Swain,

There are 2 objects in basicDetails and you want that valueString in the address property, so which value do you want to copy from both in the address property?

I don’t see the expected result, Please provide all the proper details, I don’t have any superpower to read your mind :sweat_smile:.


You can read the doc for how to ask questions and how to format the code in your post,

Hey @turivishal ,Please read my question carefully that does not required any superpower😉,I cleary write there, basicDetails[1].valueString . Then what is the meaning of ’ There are 2 objects in basicDetails and bla bla bla’…

Hi @Umasankar_Swain,

Well, there is no explanation, in your first post.

Second, your provided JSON is not a valid JSON.

I can see 2 objects in the array basicDetails, as per your line basicDetails[1].valueString, you want to access the first indexed object’s property, and there are multiple cases to cover, what if there is no object on that index?

It seems You have not gone through that doc on how to ask a question.

I am here to help you, this is for you and for your question right? So to overcome multiple conversations and back and forth if you provide proper details in your first post then you will get quick answers.

I am still open to help you if you want to talk properly.

2 Likes

Is this what are you looking for? as per my prediction.

Input:

[
  {
    "attributes": [
      {
        "basicDetails": [
          {
            "name": "Name",
            "fieldType": "textField",
            "fieldKey": "nameField",
            "fieldDependendent": "",
            "valueString": "",
            "valueArray": "",
            "valueBoolean": false
          },
          {
            "name": "Address",
            "fieldType": "textareaAddressField",
            "fieldKey": "addressField",
            "fieldDependendent": "",
            "valueString": "this address",
            "valueArray": "Test",
            "valueBoolean": false
          }
        ],
        "specificationDetails": "",
        "additionalDetails": ""
      }
    ],
    "status": "",
    "createdAt": "",
    "updatedAt": ""
  }
]

Query:

db.businesslistings.updateMany({},
[
  {
    $set: {
      address: {
        $arrayElemAt: [
          { $arrayElemAt: ["$attributes.basicDetails.valueString", 0] },
          1
        ]
      }
    }
  }
])

Updated Doc:

[
  {
    "address": "this address",
     // other properties ...
   }
]

Playground

Okay @turivishal ,Thanks for your time, this is the solution,

db.businesslistings.updateMany({},
[
  {
    $set: {
      "address": {
        $getField: {
          input: {
            $arrayElemAt: [
              {
                $getField: {
                  input: {
                    $arrayElemAt: [
                      "$attributes",
                      0
                    ]
                  },
                  field: "basicDetails"
                }
              },
              1
            ]
          },
          field: "valueString"
        }
      }
    }
  }
])

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.