wordpress拖曳式建站,正邦设计创始人,网站建设 微信,河北住房和建设厅官方网站MongoTemplate是Spring Data MongoDB提供的一个Java编程接口#xff0c;用于操作MongoDB数据库。它提供了一系列方法和功能#xff0c;以便进行数据的插入、更新、删除和查询等操作。
使用MongoTemplate#xff0c;你可以通过编写Java代码与MongoDB进行交互#xff0c;而无…MongoTemplate是Spring Data MongoDB提供的一个Java编程接口用于操作MongoDB数据库。它提供了一系列方法和功能以便进行数据的插入、更新、删除和查询等操作。
使用MongoTemplate你可以通过编写Java代码与MongoDB进行交互而无需直接编写原生的MongoDB查询语句。它提供了一些便捷的方法如save、insert、update和remove用于对文档进行增删改操作。同时它还支持复杂的查询条件和排序以及聚合管道查询等高级功能。
1 引用pom
!--mongodb-- dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-data-mongodb/artifactId /dependency
2 Aggregation 是一种强大的数据处理操作它允许你对集合中的文档进行多个阶段的处理和转换以生成复杂的结果。
使用聚合操作你可以对文档进行分组、排序、过滤、投影、计算字段值、连接多个集合等操作以实现更复杂的数据处理需求
$match 过滤数据输出符合条件文档 where $project 修改输入文档结构筛选展示文档的键 个人理解select $limit 限制计算文档结果返回数 limit $sort 文档排序 order by $group 文档分组 group by $skip 跳过指定数量的文档 skip $unwind 展开数组数组内容拆分显示 无
$facet用于在单个聚合管道中执行多个聚合操作并将结果分组输出。
3 查询方法 Override public ListTest getList(Test search) { 1 区间查询 Criteria criteriaInfo Criteria.where(Time).gte(search.getStartTime()).lte(search.getEndTime()); 2 in 查询 criteriaInfo.and(Code).in(ListString); 3 or 查询 两个满足1 criteriaInfo.orOperator( Criteria.where(title).regex(值), Criteria.where(name).regex(值) ); 4 is 等于 criteriaInfo.and(Status).is(值); 5 nin 不等于 criteriaInfo.and(Time).nin(值);
6 ne 不等于某个值的条件
Criteria.where(Status).ne(0) ListAggregationOperation aggregationList new ArrayList(); // Match操作筛选 aggregationList.add(Aggregation.match( criteriaInfo)); // 只查出 Test 中的字段 相当于 select *(字段) aggregationList.add( Aggregation.project(Time) ); Aggregation aggregation Aggregation.newAggregation(aggregationList); ListTest result; result mongoTemplate.aggregate( aggregation, Test, Test.class).getMappedResults(); return null; } 2 查询左连 Override public ListTest getList(Test search) { Criteria criteria Criteria.where(Time).gte(search.getStartTime()).lte(search.getEndTime()); ListAggregationOperation aggregationList new ArrayList(); // Match操作筛选 aggregationList.add(Aggregation.match(criteria)); // 只查出 Test 中的字段 aggregationList.add( Aggregation.project(Time) //显字段 .andExpression(title2).as(title) //替换显示字段 .andExpression(ifNull(status, 0)).as(status) //替换显示字段 ); // Lookup操作 左连接 aggregationList.add(LookupOperation.newLookup() .from(test2) //关联表名 .localField(Time) // 主表中的关联字段 .foreignField(Time) //从表关联的字段 .as(test3)); //查询结果表名 Aggregation aggregation Aggregation.newAggregation(aggregationList); ListTest result; result mongoTemplate.aggregate( aggregation, Test, Test.class).getMappedResults(); return null; } 3 查询总记数
Map total mongoTemplate.aggregate(aggregation, test, Map.class).getUniqueMappedResult();
4 分页 ListAggregationOperation aggregationList new ArrayList();
aggregationList.add(Aggregation.skip((search.getPageNum() - 1) * search.getPageSize()));
aggregationList.add(Aggregation.limit(search.getPageSize())); 5 排序
ListTest dataList new ArrayList(); dataList.sort(Comparator.comparing(Test::getTime).thenComparing(Test::getcode)); 6 then(1).otherwise(0) 是一个伪代码用来表示在数据聚合操作中的条件逻辑。它的作用是根据条件的满足与否分别赋值不同的结果
new Criteria().andOperator(Criteria.where(Code).in(值)
)).then(1).otherwise(0))
then 方法用来指定条件满足时的赋值结果otherwise 方法用来指定条件不满足时的赋值结果