Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
具有多个字段的Spring data mongoDB推送操作不起作用_Mongodb_Spring Boot_Aggregation - Fatal编程技术网

具有多个字段的Spring data mongoDB推送操作不起作用

具有多个字段的Spring data mongoDB推送操作不起作用,mongodb,spring-boot,aggregation,Mongodb,Spring Boot,Aggregation,我在MongoDB中有一组数据对象,如- [ { "status" : "passed", "version" : "124" , "value" : 6 }, { "status" : "passed", "version" : "123" , "value" : 10 }, { "status" : "failed", "version" : "123" , "value" : 16 } ] 我想把它做成这样的格式- [ { version: 12

我在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 
         }
      ]
    }
]

似乎在对象中没有获取状态的值。如何解决此问题?

我尝试使用mongo聚合。在贝壳里试试。 我不熟悉春天

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");

我也在做同样的事情,这对我来说很好。您使用的Spring data mongodb的版本是什么?谢谢您的回复,但我需要Spring boot mongodb查询。