本文介绍了无法在Spring Data MongoDb中使用嵌套的VariableOperators.mapItemsOf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被迫使用聚合框架和 Spring Data MongoDb 项目操作.

I'm forced to use the aggregation framework and the project operation of Spring Data MongoDb.

我想做的是通过项目操作来创建对象数组.

What I'd like to do is creating an array of object as a result of a project operation.

考虑此中间聚合结果:

{
  "processes": [
    {
      "id": "101a",
      "assignees": [
        {
          "id": "201a",
          "username": "carl93"
        },
        {
          "id": "202a",
          "username": "susan"
        }
      ]
    },
    {
      "id": "101b",
      "assignees": [
        {
          "id": "201a",
          "username": "carl93"
        },
        {
          "id": "202a",
          "username": "susan"
        }
      ]
    }
  ]
}

我正在尝试为每个流程获取所有受让人的用户名和ID.因此,我想要获得的是这样的:

I'm trying to get for each process, all the assignee usernames and ids. Hence, what I want to obtain is something like this:

[
  {
    "results": [
      {
        "id": "201a",
        "value": "carl93",
        "parentObjectId": "101a"
      },
      {
        "id": "202a",
        "value": "susan",
        "parentObjectId": "101a"
      },
      {
        "id": "201a",
        "value": "carl93",
        "parentObjectId": "101b"
      },
      {
        "id": "202a",
        "value": "susan",
        "parentObjectId": "101b"
      }
    ]
  }
]

要实现此目标,我使用2个嵌套的VariableOperators.mapItemsOf获取:

To reach this goal I'm using 2 nested VariableOperators.mapItemsOf obtaining:

org.springframework.data.mapping.MappingException: Cannot convert [Document{{id= 201a, value= carl93, parentObjectId= 101a}}, Document{{id= 202a, value = susan, parentObjectId= 101a}}]
of type class java.util.ArrayList into an instance of class java.lang.Object! 
Implement a custom Converter<class java.util.ArrayList, class java.lang.Object> and register it with the CustomConversions.

这是我当前正在使用的代码:

Here's the code that I'm currently using:

new ProjectionOperation().and(
    VariableOperators.mapItemsOf("processes")
      .as("pr")
      .andApply(
           VariableOperators.mapItemsOf("$pr.ownership.assignees")
               .as("ass")
               .andApply(aggregationOperationContext -> {
           Document document = new Document();

           document.append("id", "$$ass.id");
           document.append("value", "$$ass.username");
           document.append("parentObjectId", "$$pr.id");

           return document;
          })
    )
).as("results");

代码会产生以下结果:

[ 
  [
    {
      "id": "201a",
      "value": "carl93",
      "parentObjectId": "101a"
    },
    {
      "id": "202a",
      "value": "susan",
      "parentObjectId": "101a"
    }
  ], 
  [
    {
      "id": "201a",
      "value": "carl93",
      "parentObjectId": "101b"
    },
    {
      "id": "202a",
      "value": "susan",
      "parentObjectId": "101b"
    }
  ]
]

如您所见,有2个嵌套数组,[[],[]].这就是引发异常的原因.不过,我想要获得的只是一个数组,在其中添加所有对象(可能没有重复或空值).我尝试了addToSet运算符和其他聚集运算符,但没有成功.

As you can see there are 2 nested arrays, [[],[]]. This is the reason why the exception is thrown. Nevertheless what I want to obtain is just one array, adding all the objects in it (possibly without duplicates or null values). I've tried the addToSet operator and other aggregtion operators, without any success.

推荐答案

使用$reduce$concatArrays来连接数组.

 new ProjectionOperation().and(
    ArrayOperators.arrayOf("processes")
      .reduce(ArrayOperators.ConcatArrays.arrayOf("$$value").concat(
           VariableOperators.mapItemsOf("$$this.ownership.assignees")
               .as("ass")
               .andApply(aggregationOperationContext -> {
           Document document = new Document();
           document.append("id", "$$ass.id");
           document.append("value", "$$ass.username");
           document.append("parentObjectId", "$$this.id");
           return document;
          })
    )).startingWith(Arrays.asList())
).as("results");

这篇关于无法在Spring Data MongoDb中使用嵌套的VariableOperators.mapItemsOf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 22:48