Update the DataType for a field inside the nested array embedded document

Hi there,

It’s possible to do this using pipeline updates, I’ll show what it looks like in JS/mongo shell and you can translate it to C# then:

db.uptype.update(
     {"tag.tagExceptions.exceptionDate":{$type:"string"}}, 
     [{$set:{"tag.tagExceptions":{$map:{
         input:"$tag.tagExceptions", 
         in:{$cond:{
             if:{$eq:["string",{$type:"$$this.exceptionDate"}]}, 
             then:{$mergeObjects:["$$this", {exceptionDate:{$toDate:"$$this.exceptionDate"}}]}, 
             else:"$$this"
         }}
    }}}}])

Asya

2 Likes