Se a matriz de subdocumentos que você deseja omitir não for muito grande. Gostaria apenas de removê-lo no lado do aplicativo. Fazer o processamento no MongoDB significa que você escolhe usar os recursos de computação do MongoDB em vez de seu aplicativo. Geralmente, seu aplicativo é mais fácil e barato de dimensionar, portanto, a implementação na camada de aplicativo é preferível.
Mas neste caso exato não é muito complexo implementá-lo no MongoDB:
db.collection.aggregate([
{
$addFields: { // keep the first element somewhere
first: { $arrayElemAt: [ "$mainArray", 0] }
}
},
{
$project: { // remove the subdocument field
"mainArray.array": false
}
},
{
$addFields: { // join the first element with the rest of the transformed array
mainArray: {
$concatArrays: [
[ // first element
"$first"
],
{ // select elements from the transformed array except the first
$slice: ["$mainArray", 1, { $size: "$mainArray" }]
}
]
}
}
},
{
$project: { // remove the temporary first elemnt
"first": false
}
}
])
MongoDB Playground