使用MongoTemplate聚合查询结果不正确的问题?

文档结构为:
image.png

需求为:
统计指定的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();

控制器日志打印语句如下:
image.png

程序返回结果:统计为0

image.png

但直接通过查询语句查询,结果正确:

db.yzChat.aggregate(
    [{"$match": {"sessId": {"$in": [1]}, "isRead": 0}}, {"$facet": {"count": [{"$count": "total"}]}}]
)

image.png

阅读 2.4k
1 个回答

看完,估计是模型不对

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题