Aggregation.project使用filter筛选时,item为null怎么办?

ProjectionOperation projectionOperation1 = Aggregation.project("uid", "userName", "userAvatar", "action", "objectType", "objectOthers",
                "createdDt", "url_as", "userCopyUrl_as")
                .andExpression("toString(objectId)").as("objectId")
                .andExpression("toString(_id)").as("id")
.and(filter("userLikeUrl_as").as("item")
.by(ComparisonOperators.Eq.valueOf("item.valid").equalToValue(1))).as("userLikeUrl_as_list");
阅读 2.1k
2 个回答

解决了,是因为返回的对象类型使用的是int,应该使用Integer才行,因为Integer允许为Null

private Long total;
private Integer pageNum;
private Integer pageSize;
private List<HashMap> rows;
ProjectionOperation projectionOperation1 = Aggregation.project("uid", "userName", "userAvatar", "action", "objectType", "objectOthers",
        "createdDt", "url_as", "userCopyUrl_as")
        .andExpression("toString(objectId)").as("objectId")
        .andExpression("toString(_id)").as("id")
        .and(ConditionalOperators.Cond.when(Criteria.where("userLikeUrl_as.item").ne(null))
                .then(filter("userLikeUrl_as").as("item")
                        .by(ComparisonOperators.Eq.valueOf("item.valid").equalToValue(1)))
                .otherwise(new ArrayList<>()))
        .as("userLikeUrl_as_list");
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进