I was having a play and got a basic something working but not complete as tied up.
The idea is to use the $map function and $mergeObject so you can iterate over very element of the array of items, for each one you the use a $cond operator to either pull just the data or the first array element of the data, resulting in them all being a string as opposed to array.
db.getCollection("Demo4").insertOne({
stuff:[
{'name':'1', 'data':'stuff'},
{'name':'2', 'data':['stuff']},
]
})
db.getCollection("Demo4").aggregate([
{
$addFields:{
newStuff:{
$map:{
input:'$stuff',
as:'myData',
in:{
$mergeObjects:[
'$$myData',
{
'data':{
$cond:{
if:{
$eq:[
'array',
{$type:'$$myData.data'}
]
},
then:{$arrayElemAt:['$$myData.data', 0]},
else:'$$myData.data'
}
}
}
]
}
}
}
}
}
])
In the example above I’m using an aggregation and $addFields to just show the output, as well as the original. In an update you can just perform an update with aggregation format update statement, i.e. enclose the update part in array markers:
db.collection.updateMany({}, [{$set:{'A':'B'}}])