从 mongo 聚合中获取 java 原语,没有新的输出 class
Obtain java primitive from mongo aggregation without a new output class
我有一个聚合:
AggregationResults<Integer> result = mongoTemplate.aggregate(
Aggregation.newAggregation(
Aggregation.group().count().as("value"),
Aggregation.project("value").andExclude("_id"),
MyData.class, Integer.class);
在 mongo shell 中,当我不必映射对象时,我得到:{ "value" : 2 }
但是,我在尝试映射这个单独的值时遇到以下错误:org.springframework.data.mapping.model.MappingException: No mapping metadata found for java.lang.Integer
当我只想获得一个 java 基元时,是否可以绕过必须创建新的输出类型 class?
注意:对于此处所述的分片错误,我将采用这种方法而不是 db.collection.count() - https://docs.mongodb.com/manual/reference/method/db.collection.count/#sharded-clusters
AggregationResults<DBObject> result = mongoTemplate.aggregate(
Aggregation.newAggregation(
Aggregation.group().count().as("value"),
Aggregation.project("value").andExclude("_id"),
MyData.class, DBObject.class);
int count = (Integer) result.getUniqueMappedResult().get("value");
所以,不完全是我想要的,因为我仍然需要遍历一个对象,但它并没有比我以前的代码多,我不需要再做一个 class 作为 outputType .
我有一个聚合:
AggregationResults<Integer> result = mongoTemplate.aggregate(
Aggregation.newAggregation(
Aggregation.group().count().as("value"),
Aggregation.project("value").andExclude("_id"),
MyData.class, Integer.class);
在 mongo shell 中,当我不必映射对象时,我得到:{ "value" : 2 }
但是,我在尝试映射这个单独的值时遇到以下错误:org.springframework.data.mapping.model.MappingException: No mapping metadata found for java.lang.Integer
当我只想获得一个 java 基元时,是否可以绕过必须创建新的输出类型 class?
注意:对于此处所述的分片错误,我将采用这种方法而不是 db.collection.count() - https://docs.mongodb.com/manual/reference/method/db.collection.count/#sharded-clusters
AggregationResults<DBObject> result = mongoTemplate.aggregate(
Aggregation.newAggregation(
Aggregation.group().count().as("value"),
Aggregation.project("value").andExclude("_id"),
MyData.class, DBObject.class);
int count = (Integer) result.getUniqueMappedResult().get("value");
所以,不完全是我想要的,因为我仍然需要遍历一个对象,但它并没有比我以前的代码多,我不需要再做一个 class 作为 outputType .