Si la matriz de subdocumentos que desea omitir no es muy grande. Simplemente lo eliminaría en el lado de la aplicación. Procesar en MongoDB significa que elige usar los recursos informáticos de MongoDB en lugar de su aplicación. Por lo general, su aplicación es más fácil y económica de escalar, por lo que es preferible la implementación en la capa de la aplicación.
Pero en este caso exacto no es muy complejo implementarlo en 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
}
}
])