【发布时间】:2021-11-19 08:26:22
【问题描述】:
我正在尝试使用 Mongo 聚合,但收到一个我不理解的错误。
这是我的域名:
@Document(collection = "tapes")
public class Tape {
@Id
private String id;
private String area;
private Integer tape;
private String tapeModel;
// follow getters e setters
mongo shell 命令和输出如下:
> db.tapes.aggregate([{ $group: { _id: { "area":"$area"}, tapes: {$push: {tape: "$tape"}}}} ])
{ "_id" : { "area" : "free" }, "tapes" : [ { "tape" : 1 }, { "tape" : 2 } ] }
{ "_id" : { "area" : "Qnap" }, "tapes" : [ { "tape" : 3 } ] }
以下是在 Spring 中重新创建聚合的尝试:
AggregationOperation group = Aggregation.group("area").push("tape").as("tape");
Aggregation aggregation = Aggregation.newAggregation(group);
AggregationResults<Tape> results = mongoTemplate.aggregate(aggregation, "tapes", Tape.class);
//List<Tape> tapes = mongoTemplate.aggregate(aggregation, mongoTemplate.getCollectionName(Tape.class), Tape.class).getMappedResults();
List<Tape> tapes = results.getMappedResults();
System.out.println(tapes);
但我收到以下错误:
Cannot convert [3] of type class java.util.ArrayList into an instance of class java.lang.Integer! Implement a custom Converter<class java.util.ArrayList, class java.lang.Integer> and register it with the CustomConversions. Parent object was: it.unifi.cerm.cermadminspring.domain.Tape@2b84da07 -> null
org.springframework.data.mapping.MappingException: Cannot convert [3] of type class java.util.ArrayList into an instance of class java.lang.Integer! Implement a custom Converter<class java.util.ArrayList, class java.lang.Integer> and register it with the CustomConversions. Parent object was: it.unifi.cerm.cermadminspring.domain.Tape@2b84da07 -> null
我不明白为什么,我搜索了聚合示例,所有示例都或多或少与我的相似。
有人可以帮助我吗?
【问题讨论】:
标签: spring spring-data-mongodb