Update the DataType for a field inside the Nested Array

Hello Everyone, In the below mentioned document: I would like to update the tagSerialNumber to int64.

Please help me in updating the datatype

{
  "_id": {
    "$oid": "635286ea1c66064140400d67"
  },  
  "retailerRequestId": 82,  
  "receive": [
    {
      "receivedQty": 100,
      "receivedDate": "2015-12-01 18:16:46",
      "receivedStatus": {
        "statusId": 435,
        "status": "RReceivedFull"
      },
      "tagItems": [
        {
          "tagSerialNumber": "137438955172",
          "status": "RReceivedFull"
        },
        {
          "tagSerialNumber": "137438955171",
          "status": "RReceivedFull"
        },
        {
          "tagSerialNumber": "137438955170",
          "status": "RReceivedFull"
        },
        {
          "tagSerialNumber": "137438955169",
          "status": "RReceivedFull"
        },
        {
          "tagSerialNumber": "137438955168",
          "status": "RReceivedFull"
        }
      ]
    }
  ]
}

Hello @Amarendra_Krishna ,

You can use an update with aggregation pipeline starting from MongoDB 4.2,

  • $map to iterate loop of receive array
  • $mergeObjects to merge current object with updated property
  • $toLong to convert the type of tagSerialNumber to string
db.collection.updateMany(
  { "receive.tagItems.tagSerialNumber": { $type: "string" } },
  [{
    $set: {
      receive: {
        $map: {
          input: "$receive",
          in: {
            $mergeObjects: [
              "$$this",
              {
                tagItems: {
                  $map: {
                    input: "$$this.tagItems",
                    in: {
                      $mergeObjects: [
                        "$$this",
                        { tagSerialNumber: { $toLong: "$$this.tagSerialNumber" } }
                      ]
                    }
                  }
                }
              }
            ]
          }
        }
      }
    }
  }]
)

Playground

Warning: Should test in development environment first before production.

2 Likes

Thank you so much, this worked for me

can u help me? I’m trying to change double to decimal inside arrays, but for me it doesnt work :

 dbTenant.getCollection("LoanRequest").updateMany({}, [
    {
      $set: {
        Proposal: {
          $map: {
            input: "$Proposal",
            in: {
              $mergeObjects: [
                "$$this",
                {
                  ProposalTaxes: {
                    $map: {
                      input: "$$this.ProposalTaxes",
                      in: {
                        $mergeObjects: [
                          "$$this",
                          { IOF: { $toInt: "$$this.IOF" } },
                        ],
                      },
                    },
                  },
                },
              ],
            },
          },
        },
      },
    },
  ]);

Failed to execute script.

Error:
WriteError({
	"index" : 0,
	"code" : 16883,
	"errmsg" : "input to $map must be an array not object",
	"op" : {
		"q" : {
			
		},
		"u" : [
			{
				"$set" : {
					"Proposal" : {
						"$map" : {
							"input" : "$Proposal",
							"in" : {
								"$mergeObjects" : [
									"$$this",
									{
										"ProposalTaxes" : {
											"$map" : {
												"input" : "$$this.ProposalTaxes",
												"in" : {
													"$mergeObjects" : [
														"$$this",
														{
															"IOF" : {
																"$toInt" : "$$this.IOF"
															}
														}
													]
												}
											}
										}
									}
								]
							}
						}
					}
				}
			}
		],
		"multi" : true,
		"upsert" : false
	}
}) :
WriteError({
	"index" : 0,
	"code" : 16883,
	"errmsg" : "input to $map must be an array not object",
	"op" : {
		"q" : {
			
		},
		"u" : [
			{
				"$set" : {
					"Proposal" : {
						"$map" : {
							"input" : "$Proposal",
							"in" : {
								"$mergeObjects" : [
									"$$this",
									{
										"ProposalTaxes" : {
											"$map" : {
												"input" : "$$this.ProposalTaxes",
												"in" : {
													"$mergeObjects" : [
														"$$this",
														{
															"IOF" : {
																"$toInt" : "$$this.IOF"
															}
														}
													]
												}
											}
										}
									}
								]
							}
						}
					}
				}
			}
		],
		"multi" : true,
		"upsert" : false
	}
})
WriteError@src/mongo/shell/bulk_api.js:458:48
mergeBatchResults@src/mongo/shell/bulk_api.js:855:49
executeBatch@src/mongo/shell/bulk_api.js:919:13
Bulk/this.execute@src/mongo/shell/bulk_api.js:1163:21
DBCollection.prototype.updateMany@src/mongo/shell/crud_api.js:690:17
@(shell):4:3
DBQuery.prototype.forEach@src/mongo/shell/query.js:494:9
@(shell):1:1

Hello @Thais_Caldoncelli_Nogueira, Welcome to the MongoDB community forum,

I would suggest you ask this in a new topic and mention the current topic link if this is related.