文档结构为:
需求为:
统计指定的sessId、isRead为0的总数
问题表现:
代码中建立查询的代码为
// sessIdList为查询出来的一个List<Long>集合
final Criteria sessIdCriteria = Criteria.where("sessId").in(sessIdList);
final Aggregation aggregation = Aggregation.newAggregation(
match(sessIdCriteria.and("isRead").is(Byte.valueOf("0"))),
Aggregation.facet(count().as("total")).as("count")
);
final AggregationResults<HashMap> user = mongoTemplate.aggregate(aggregation, "user", HashMap.class);
final List<HashMap> mappedResults = user.getMappedResults();
控制器日志打印语句如下:
程序返回结果:统计为0
但直接通过查询语句查询,结果正确:
db.yzChat.aggregate(
[{"$match": {"sessId": {"$in": [1]}, "isRead": 0}}, {"$facet": {"count": [{"$count": "total"}]}}]
)
看完,估计是模型不对