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”
},
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 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.
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.