Spring MongoDB 的嵌套组

Nested Group with Spring MongoDB

我需要为每个用户生成一个包含每个级别的警报数量的结果。

类似于下面的结构:

{
  "identitity": "59e3b9dc5a3254691f327b67",
  "alerts": [
    {
      "level": "INFO",
      "count": "3"
    },
    {
      "level": "ERROR",
      "count": "10"

     } 
  ]

}

警报实体具有以下结构:

@Document(collection = AlertEntity.COLLECTION_NAME)
public class AlertEntity {

    public final static String COLLECTION_NAME = "alerts";

    @Id
    private ObjectId id;

    @Field
    private AlertLevelEnum level = AlertLevelEnum.INFO;

    @Field("title")
    private String title;

    @Field("payload")
    private String payload;

    @Field("create_at")
    private Date createAt = new Date();

    @Field("delivered_at")
    private Date deliveredAt;

    @Field("delivery_mode")
    private AlertDeliveryModeEnum deliveryMode = 
   AlertDeliveryModeEnum.PUSH_NOTIFICATION;

    @Field("parent")
    @DBRef
    private ParentEntity parent;

    @Field("son")
    @DBRef
    private SonEntity son;

    private Boolean delivered = Boolean.FALSE;

}

我已经实现了以下方法,试图以嵌套方式投影结果。但是 "Identity" 字段始终为 null,而 "alerts" 字段是一个空集合。

@Override
    public List<AlertsBySonDTO> getAlertsBySon(List<String> sonIds) {


        TypedAggregation<AlertEntity> alertsAggregation = 
                Aggregation.newAggregation(AlertEntity.class,
                    Aggregation.group("son.id", "level").count().as("count"),
                    Aggregation.project().and("son.id").as("id")
                        .and("alerts").nested(
                                bind("level", "level").and("count")));

        // Aggregation.match(Criteria.where("_id").in(sonIds)

            AggregationResults<AlertsBySonDTO> results = mongoTemplate.
                 aggregate(alertsAggregation, AlertsBySonDTO.class);

            List<AlertsBySonDTO> alertsBySonResultsList = results.getMappedResults();

            return alertsBySonResultsList;
    }  

我得到的结果如下:

{
  "response_code_name": "ALERTS_BY_SON",
  "response_status": "SUCCESS",
  "response_http_status": "OK",
  "response_info_url": "http://yourAppUrlToDocumentedApiCodes.com/api/support/710",
  "response_data": [
    {
      "identity": null,
      "alerts": []
    },
    {
      "identity": null,
      "alerts": []
    }
  ],
  "response_code": 710
}

DTO结果如下:

public final class AlertsBySonDTO implements Serializable {

    private static final long serialVersionUID = 1L;


    @JsonProperty("identity")
    private String id;

    @JsonProperty("alerts")
    private ArrayList<Map<String, String>> alerts;


    public AlertsBySonDTO() {
        super();
    }

    public AlertsBySonDTO(String id, ArrayList<Map<String, String>> alerts) {
        super();
        this.id = id;
        this.alerts = alerts;
    }



    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public ArrayList<Map<String, String>> getAlerts() {
        return alerts;
    }

    public void setAlerts(ArrayList<Map<String, String>> alerts) {
        this.alerts = alerts;
    }
}

以嵌套方式投影结果需要做什么?

提前致谢

在聚合框架中,有一个 $unwind 运算符,它基本上会将具有两个元素的嵌套数组的一个元素集合转换为两个单独的文档,其中一个元素来自此大批。所以你会得到:

{
 "identitity": "59e3b9dc5a3254691f327b67",
 "alerts": {
     "level": "INFO",
     "count": "3"
   }

}

{
     "identitity": "59e3b9dc5a3254691f327b67",
     "alerts": {
         "level": "ERROR",
         "count": "10"
     } 
}

这是您可以从计数开始分组的地方。应该可以正常工作。