How to do $group? to subdocument by mongoDB

problem description

two Collection: teams , scores .
teams score is an Array, field associated with _ id .
now look up teams and count the score according to its round field.
but I can only look up all the relevant values from scores Collection. I don"t know how to count them.

The structure of

teams is as follows:

/* 1 */
{
    "_id" : ObjectId("5b963f96219e2651faaf1d1b"),
    "title" : "",
    "number" : "01",
    "description" : "",
    "score" : [ 
        ObjectId("5b9a1ced72b68d055a2f56e6"), 
        ObjectId("5b9a1d6cdb831f08d251c24c"), 
        ObjectId("5b9a1dbedb831f08d251c24d"), 
    ]
}

/* 2 */
{
    "_id" : ObjectId("5b963f96219e2651faaf1d1c"),
    "title" : "",
    "number" : "02",
    "description" : "",
    "score": []
}

scores structure is as follows:

/* 1 */
{
    "_id" : ObjectId("5b965604e9095d7cfb289ea8"),
    "team" : ObjectId("5b963f96219e2651faaf1d1b"),
    "round" : ObjectId("5b8e5467759a584980dbe33b"),
    "value" : 22,
}

/* 2 */
{
    "_id" : ObjectId("5b965610e9095d7cfb289ea9"),
    "team" : ObjectId("5b963f96219e2651faaf1d1b"),
    "round" : ObjectId("5b8e5467759a584980dbe33c"),
    "value" : 221,
}

/* 3 */
{
    "_id" : ObjectId("5b965614e9095d7cfb289eaa"),
    "team" : ObjectId("5b963f96219e2651faaf1d1b"),
    "round" : ObjectId("5b8e5467759a584980dbe33b"),
    "value" : 98,
}

what methods have you tried? related code

db.getCollection("teams").aggregate([
  {
    $lookup: {
      from: "scores",
      localField: "score",
      foreignField: "_id",
      as: "score"
    }
  }
])

I can use the above code to find out the following data:

{
    "_id" : ObjectId("5b963f96219e2651faaf1d1b"),
    "title" : "",
    "number" : "01",
    "description" : "",
    "score" : [ 
        {
            "_id" : ObjectId("5b9a1ced72b68d055a2f56e6"),
            "team" : ObjectId("5b963f96219e2651faaf1d1b"),
            "round" : ObjectId("5b8e5467759a584980dbe33b"),
            "value" : 228
        }, 
        {
            "_id" : ObjectId("5b9a1d6cdb831f08d251c24c"),
            "team" : ObjectId("5b963f96219e2651faaf1d1b"),
            "round" : ObjectId("5b8e5467759a584980dbe33b"),
            "value" : 18,
        }, 
        {
            "_id" : ObjectId("5b9a1dbedb831f08d251c24d"),
            "team" : ObjectId("5b963f96219e2651faaf1d1b"),
            "round" : ObjectId("5b8e5467759a584980dbe33c"),
            "value" : 128,
        },
    ]
}

what result do you expect?

you can count round in the score field, for example:

{
    "_id" : ObjectId("5b963f96219e2651faaf1d1b"),
    "title" : "",
    "number" : "01",
    "description" : "",
    "score" : [ 
        {
            "round" : ObjectId("5b8e5467759a584980dbe33c"),
            "count": 2,
            "total" : 228
        }, 
        {
            "round" : ObjectId("5b8e5467759a584980dbe33b"),
            "count": 1,
            "total" : 1328,
        }, 
    ]
}

I can provide database files if necessary.
mongoDB version: 3.6.x, which can be upgraded.

provide ideas or pseudocode / code is fine, thank you.

Jun.21,2021
Menu