MongoDB 中的 $group 命令问题

Issue with $group command in MongoDB

我在使用 Mongo 数据库时遇到问题,我尝试使用以下命令对结果进行分组。这映射到包含字段 id、名称和结果列表的 Java 对象。但是,对于 me.The 文档中确实存在 $name 属性,name 属性将无法正确映射,因此我不确定是什么导致了问题。如果有人能指出正确的方向,我将不胜感激。

{
    "$group": {
    "_id": "$testCaseId",
         "name": "$name",
         "results": {
              "$push": {
                   "testCaseId": "$testCaseId",
                   "executionId": "$executionId",
                   "resultCode": "$resultCode",
                   "time": "$time"
               }
          }
     }
}

这是我要映射到的 Java 对象的结构:

private String id;
private String name;
private List<Results> results;

并且 id 和结果被正确填充(如果我删除名称),但是当我包含名称时,我得到这个异常:

"exception: the group aggregate field 'name' must be defined as an expression inside an object".

请使用 $first$last 等组累加运算符。

$first: Returns 每个组的第一个文档的值。 $last:Returns 每个组的最后一个文档的值。

{
    "$group": {
        "_id": "$testCaseId",
        "name": {"$first" : "$name"},
        "results": {
            "$push": {
                "testCaseId": "$testCaseId",
                "executionId": "$executionId",
                "resultCode": "$resultCode",
                "time": "$time"
            }
        }
    }
}