java - 如何通过 Spring Boot 获取 mongoDB 中的计数?
问题描述
我是 MongoDB 新手,所以如果我问一些愚蠢的问题,请合作。请参考下表
status:"Failure"
key:object
accountID:"AKRRTTY"
谁能告诉我如何根据 acountID (这是键的子对象)找到状态计数?
状态可以是不同的类型:例如Processed
,Blocked
等
解决方案
您必须使用聚合来一次性获取所有状态的计数。
您将需要调整输出 pojo 以考虑聚合查询。理想情况下,您的 pojo 应该包含两个字段,一个用于状态 (_id),一个用于计数 (count)。
就像是
AggregationOperation match = Aggregation.match(Criteria.where("key.carrier").is(carrierId));
AggregationOperation group = Aggregation.group("status").count().as("count");
AggregationOperation project = Aggregation.project("count").andExclude("_id").andInclude(Fields.from(Fields.field("status", "_id")));
Aggregation agg = Aggregation.newAggregation(match,group,project);
List<YourPojo> results = mongoTemplate.aggregate(agg, colname, YourPojo.class).getMappedResult();
推荐阅读
- racket - 使用循环携带依赖简化循环
- css - 无法居中按钮
- javascript - 选中复选框时,acrobat javascript,需要单独的字段
- c++ - 如何使用 GMock 的 Field() 宏来匹配结构/联合的嵌套成员?
- geoserver - 在地理服务器中找不到 imagepyramid 的 sample_image
- azure - Azure CI/CD - Azure 私有 Npm 注册表 - 无法进行身份验证,需要:Bearer authorization_uri=https://login.windows.net
- perl - 有没有办法设置 XML::LibXML->load_html(location => $url) 的连接超时
- jmeter - 我创建了一个由 10 个用户组成的步进线程组,但在运行计划后它没有显示用户数,也没有停止在 10 个用户
- popupwindow - 弹出窗口显示另一个程序正在使用此文件的错误
- html - 使第二行文本在第一行文本下对齐