Spring 数据 mongoDB 多个字段的推送操作不起作用

Spring data mongoDB push operation with multiple fields are not working

我在 MongoDB 中有一组数据对象,因为-

[
    { "status" : "passed", "version" : "124" , "value" : 6 },
    { "status" : "passed", "version" : "123" , "value" : 10 },
    { "status" : "failed", "version" : "123" , "value" : 16 }
]

我想以类似 -

的格式获取它
[
    { 
    version: 124,
    "series" :[ 
        { 
            "name" : "passed", 
            "value" : 6 
        }
      ]
    },
    {
    version: 123,
    "series" : [
         { 
             "name" : "passed", 
             "value" : 10 
         },
         { 
             "name" : "failed", 
             "value" : 16 
         }
      ]
    }
]

我应该如何编写查询? 我写了

这样的查询

我写了这样的查询:

Aggregation.group("version").push(new BasicDBObject("name","$status").append("value", "$value")).as("series");

使用上面的聚合查询,我得到这样的结果:

[
    { 
    version: 124,
    "series" :[ 
        { 
          "name" : null, 
          "value" : 6 
        }
      ]
    },
    {
    version: 123,
    "series" : [
         { 
           "name" : null, 
           "value" : 10
         },
         { 
           "name" : null, 
           "value" : 16 
         }
      ]
    }
]

好像没有在object中取status的值。我该如何解决这个问题?

我尝试使用 mongo 聚合。在 shell 中尝试。 我不熟悉spring。

db.getCollection('Test1').aggregate([
{
$group:{
    _id:"$version",
    version:{$first:"$version"},
    series:{
        $push:{
            name:"$status",
            value:"$value"
            }
        }
    }
},
{$project:{_id:0}}
])

我的查询是这样的:

db.results.aggregate([{
     {
       "$group": {
             "_id": {
                  "status": "$status",
                  "version": "$version",
              },
             "count": {
                  "$sum": 1
              }
            }
        }, {
            $group: {
                _id: "$_id.version",
                "series": {
                    $push: {
                        "status": "$_id.status",
                        "value": "$count"
                    }
                }
            }
        }
    ]);

所以我必须将聚合查询添加为:

Aggregation.group("version").push(new BasicDBObject("_id", "$_id.status").append("value", "$value")).as("series");