Update the Key in the nested array

Hello Everyone, For the below mentioned document i would like to update the StatusDate from String to Date. Please help me in updating the DataType.

{
“_id”: {
“$oid”: “635f7c5b2cc3505680eb3006”
},
“history”: [
{
“status”: {
“statusId”: 1,
“statusDate”: “2015-10-13 14:08:01”
},
“location”: {
“locationId”: 21,
“status”: “Shipped to Master”
},
“retailer”: {
“retailerId”: 10003775,
“retailerType”: {
“typeId”: 3010,
“name”: “SalesMasterRetailer”
}
}
}
]
}

That would be the same way as it was answered to you in

Hello,

Thanks for quick response, i used the below mentioned query: db.InventoryHistory.update(
{“history.status.statusDate”:{$type:“string”}},
[{$set:{“history.status”:{$map:{
input:"$history.status",
in:{$cond:{
if:{$eq:[“string”,{$type:"$$this.statusDate"}]},
then:{$mergeObjects:["$$this", {statusDate:{$toDate:"$$this.statusDate"}}]},
else:"$$this"
}}
}}}}])

But post update, status changed from object to array. Please find the below mentioned document updated to, after updating the collection:
{
“_id”: {
“$oid”: “636236bd2cc35012f45932de”
},

“hexTagId”: “918907048020000003E9”,
“tagSerialNo”: “137438954473”,
“history”: [
{
“status”: [
{
“statusId”: 1,
“status”: “INVENTORYNEW”,
“statusDate”: {
“$date”: {
“$numberLong”: “1444745265000”
}
}
}
]
}
]
}

Here i don’t want it to be convert from object to array. Please let me know your inputs.

Please read Formatting code and log snippets in posts and update your sample documents and pipeline so it is easier to understand and to cut-n-paste for experimentation.

Please do that for your other thread too.

Please find the below mentioned query is used:
db.InventoryHistory.
update(
{“history.status.statusDate”:{$type:“string”}},
[
{$set:{“history.status”:{$map:{
input:"$history.status",
in:{
$cond:{
if:{$eq:[“string”,{$type:"$$this.statusDate"}]},
then:{$mergeObjects:["$$this", {statusDate:{$toDate:"$$this.statusDate"}}]},
else:"$$this"
}
}}}}
}
]
)

But post update, status changed from object to array. Please find the below mentioned document after updating the collection:

{
“_id”:{
“$oid”:“636236bd2cc35012f45932de”
},
“hexTagId”:“918907048020000003E9”,
“tagSerialNo”:“137438954473”,
“history”:[
{
“status”:[
{
“statusId”:1,
“status”:“INVENTORYNEW”,
“statusDate”:{
“$date”:{
“$numberLong”:“1444745265000”
}
}
}
]
}
]
}

Please let me know how to update the collection, without changing into array.

As mentioned in your other post.

Sorry, but you published exactly the same badly formatted documents and code.

Please

Please find the below mentioned query is used:

db.InventoryHistory.
update(
		{“history.status.statusDate”:{$type:“string”}},
			[
				{$set:{“history.status”:{$map:{
					input:"$history.status",
						in:{
							$cond:{
								if:{$eq:[“string”,{$type:"$$this.statusDate"}]},
								then:{$mergeObjects:["$$this", {statusDate:{$toDate:"$$this.statusDate"}}]},
								else:"$$this"
								  }
							}}}}
				}
			]
	  )

But post update, status changed from object to array. Please find the below mentioned document after updating the collection:

{
   "_id":{
      "$oid":“636236bd2cc35012f45932de”
   },
   "hexTagId":“918907048020000003E9”,
   "tagSerialNo":“137438954473”,
   "history":[
      {
         "status":[
            {
               "statusId":1,
               "status":"INVENTORYNEW",
               "statusDate":{
                  "$date":{
                     "$numberLong":“1444745265000”
                  }
               }
            }
         ]
      }
   ]
}

Please let me know how to update the collection, without changing into array.

If you look at the $map documentation, you will see it

returns an array with the applied results

By doing

you explicitly ask to $set history.status to an array. So it is totally normal that

Most likely you do not $set/$map the appropriate field. But since your sample input document is not formatted correctly we are not to cut-n-paste it to experiment.