Можно попробовать запрос агрегации ниже:
db.collection.aggregate([
/** Match docs where both fields exists */
{
$match: {
"y2000": {
$exists: true
},
"y2001": {
$exists: true
}
}
},
/** Group branches & sum up each fields values across all docs */
{
$group: {
_id: "$name",
y2000: {
$sum: "$y2000"
},
y2001: {
$sum: "$y2001"
}
}
},
/** Transform fields to required format */
{
$project: {
_id: 0,
x: "$_id",
y2000: 1,
y2001: 1
}
}
])
Тест: MongoDB-Playground