如何在Spring MongoDB聚合中写入单个子ID字段 [英] How to write single sub id field in spring mongodb aggregation

查看:85
本文介绍了如何在Spring MongoDB聚合中写入单个子ID字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Spring Data Mongo Aggregation获得以下查询:

I am trying to get following query from Spring Data Mongo Aggregation:

{
  "_id": {
    "year": "$_id.year"
  },
  "result": {
    "$push": {
      "rs": "$_id.rs",
      "vl": "$vl"
    }
  }
}

我尝试了以下操作:

GroupAggregation as = group( "_id.year" ).push( new BasicDBObject( "rs", "$_id.rs" ).append( "vl", "$vl" ) ).as( "result" );

但是它会生成此表达式,其中"_id"字段下没有子字段"year":

But it generates this expression where there is no sub field "year" under "_id" field:

{ "$group" : { "_id" : "$_id.year" , "result" : { "$push" : { "rs" : "$_id.rs" , "vl" : "$vl"}}}}  

关于如何使用spring数据mongodb聚合获取该查询的任何想法?

Any ideas on how to get that query using spring data mongodb aggregation?

推荐答案

我看不到api中的任何钩子来访问先前的 _id 值.

I don't see any hook in the api to access the previous _id value.

您可以使用 AggregationOperation 使用mongodb类型创建 $ group 阶段.

You can use AggregationOperation to create $group stage using mongodb types.

类似

AggregationOperation group = new AggregationOperation() {
       @Override
       public DBObject toDBObject(AggregationOperationContext aggregationOperationContext) {
          return new BasicDBObject("$group", new BasicDBObject("_id", new BasicDBObject("year", "$_id.year" )).append("result", new BasicDBObject("$push", new BasicDBObject( "rs", "$_id.rs" ).append( "vl", "$vl" ))));
    } 
};

这篇关于如何在Spring MongoDB聚合中写入单个子ID字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆