【问题标题】:Nested Group with Spring MongoDB使用 Spring MongoDB 的嵌套组
【发布时间】:2018-03-27 08:16:58
【问题描述】:

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

类似于以下的结构:

{
  "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”字段始终为空,“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;
    }
}

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

提前致谢

【问题讨论】:

    标签: spring mongodb aggregation-framework spring-mongo spring-mongodb


    【解决方案1】:

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

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

    }

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

    您可以在这里使用 count 开始您的小组。应该可以正常工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-01
      • 1970-01-01
      • 2014-10-29
      • 1970-01-01
      • 2018-10-22
      相关资源
      最近更新 更多