Mongodb相关学习
Maven依赖
<!-- mongodb -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
版本同springboot吧
yml配置
spring:
data:
mongodb:
host: ip
port: 27017
username: user
password: reader
database: database
具体操作:
构建实体类:
@Data
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
// 数据库表名
@TableName("cont_standing_book_canal")
// mongo集合名(对应数据库表名)数据库按照_划分但是mongo更贴近与实体,
实体长什么样存进去字段、表名就长什么样
@Document(collection = "contStandingBookCanal")
@ApiModel(value = "ContStandingBookCanal对象", description = "合同台账统计表对比表")
public class ContStandingBookCanal implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "主键ID")
// 对应mongo字段名,实体名称可以与mongo不一样
@Field("_id")
// 指定mongo集合中的主键id
@MongoId
private Long id;
@ApiModelProperty(value = "合同号")
private String contNo;
@ApiModelProperty(value = "租户编号")
private String companyCode;
@ApiModelProperty(value = "一级业态名称")
private String oneLevelLayoutName;
}
由于mongo中默认的_id是不支持long类型的而且当id存入mongo中会自动转换成
_id
导致报错,如果实在需要使用long类型可以在插入时主动生成uuid然后插入实体进而插入到数据库中
mongoTemplate插入
mongoTemplate.insert(contStandingBooks, "contStandingBookCanal");
mongoTemplate更新
public void updateBatch(List<ContStandingBookVo> vos) {
List<ContStandingBookCanal> contStandingBooks = contStandingBookConvert.toPoList(vos);
List<Pair<Query, Update>> list = new ArrayList<>();
if (contStandingBooks.size() > 0) {
for (ContStandingBookCanal contStandingBook : contStandingBooks) {
if (!StrUtil.isNotBlank(contStandingBook.getPoint())) {
continue;
}
Query query = new Query();
Criteria criteria = new Criteria();
if (StrUtil.isNotBlank(contStandingBook.getFeeCode())) {
criteria.and("feeCode").is(contStandingBook.getFeeCode());
}
if (StrUtil.isNotBlank(contStandingBook.getContNo())) {
criteria.and("contNo").is(contStandingBook.getContNo());
}
if (StrUtil.isNotBlank(contStandingBook.getPeriodNo())) {
criteria.and("periodNo").is(contStandingBook.getPeriodNo());
}
if (StrUtil.isNotBlank(contStandingBook.getThreeLevelLayoutCode())) {
criteria.and("threeLevelLayoutCode").is(contStandingBook.getThreeLevelLayoutCode());
}
if (ObjectUtil.isNotEmpty(contStandingBook.getCycleStartDate())) {
criteria.and("cycleStartDate").is(contStandingBook.getCycleStartDate());
}
if (ObjectUtil.isNotEmpty(contStandingBook.getCycleEndDate())) {
criteria.and("cycleEndDate").is(contStandingBook.getCycleEndDate());
}
query.addCriteria(criteria);
Update update = new Update();
update.set("point", contStandingBook.getPoint());
Pair<Query, Update> pair = Pair.of(query, update);
list.add(pair);
}
BulkOperations operations = mongoTemplate.bulkOps(BulkOperations.BulkMode.UNORDERED, ContStandingBookCanal.class);
operations.updateMulti(list);
operations.execute();
}
}
mongoTemplate查询
public Integer selectTotalCount(ContStandingBookReqDto dto) {
long start = System.currentTimeMillis();
Aggregation aggregation = Aggregation.newAggregation(
Aggregation.match(getCriteria(dto)),
Aggregation.count().as("countNum")
);
AggregationResults<Map> aggregationResults = mongoTemplate.aggregate(
aggregation,
"contStandingBookCanal",
Map.class
);
log.info("totalpage查询时长:" + (System.currentTimeMillis() - start) / 1000);
if (CollectionUtil.isEmpty(aggregationResults.getMappedResults())) {
return 0;
}
return (Integer) aggregationResults.getMappedResults().get(0).get("countNum");
}
public List<ContStandingBookCanalPo> selectByPage(IPage page, ContStandingBookReqDto dto) {
long start = page.getCurrent() > 0 ? (page.getCurrent() - 1) * page.getSize() : 0;
Aggregation aggregation = Aggregation.newAggregation(
Aggregation.match(getCriteria(dto)),
Aggregation.skip(start),
Aggregation.limit(page.getSize())
);
AggregationResults<ContStandingBookCanalPo> aggregationResults = mongoTemplate.aggregate(
aggregation,
"contStandingBookCanal",
ContStandingBookCanalPo.class
);
return aggregationResults.getMappedResults();
}
public static Criteria getCriteria(ContStandingBookReqDto dto) {
Criteria criteria = new Criteria();
if (CollectionUtils.isEmpty(dto.getUserMallCodes())) {
throw new AmpCloudBizException("4445", "用户暂无项目权限");
}
if (CollectionUtils.isNotEmpty(dto.getMallCodeList())) {
criteria.and("mallCode").in(CollectionUtil.intersection(dto.getUserMallCodes(), dto.getMallCodeList()));
} else {
criteria.and("mallCode").in(dto.getUserMallCodes());
}
if (CollectionUtils.isNotEmpty(dto.getContStatus())) {
criteria.and("status").in(dto.getContStatus());
}
if (CollectionUtils.isNotEmpty(dto.getBlockCode())) {
criteria.and("blockCode").in(dto.getBlockCode());
}
if (CollectionUtils.isNotEmpty(dto.getFloorCode())) {
criteria.and("floorCode").in(dto.getFloorCode());
}
if (ObjectUtil.isNotEmpty(dto.getContType())) {
criteria.and("contType").is(dto.getContType());
}
if (CollectionUtils.isNotEmpty(dto.getContStoreType()) || CollectionUtils.isNotEmpty(dto.getAuthStoreType())) {
criteria.and("contStoreType").in(CollectionUtil.intersection(dto.getAuthStoreType(), dto.getContStoreType()));
}
if (StrUtil.isNotBlank(dto.getSearchShow())) {
Pattern pattern = Pattern.compile("^.*" + dto.getSearchShow() + ".*$");
criteria.orOperator(
Criteria.where("brandName").regex(pattern),
Criteria.where("companyName").regex(pattern),
Criteria.where("storeNos").regex(pattern),
Criteria.where("contNo").regex(pattern)
);
}
if (CollectionUtils.isNotEmpty(dto.getOneLevelLayoutCode())) {
criteria.and("oneLevelLayoutCode").in(dto.getOneLevelLayoutCode());
}
if (CollectionUtils.isNotEmpty(dto.getRentType())) {
criteria.and("rentType").in(dto.getRentType());
}
if (CollectionUtils.isNotEmpty(dto.getFeeCode())) {
criteria.and("feeCode").in(dto.getFeeCode());
}
if (ObjectUtil.isNotEmpty(dto.getContBeginDateStart()) && ObjectUtil.isNotEmpty(dto.getContBeginDateEnd())) {
criteria.and("contBeginDate").gte(dto.getContBeginDateStart()).lt(dto.getContBeginDateEnd());
}
if (ObjectUtil.isNotEmpty(dto.getContEndDateStart()) && ObjectUtil.isNotEmpty(dto.getContEndDateEnd())) {
criteria.and("contEndDate").gte(dto.getContEndDateStart()).lt(dto.getContEndDateEnd());
}
if (ObjectUtil.isNotEmpty(dto.getContFailDateStart()) && ObjectUtil.isNotEmpty(dto.getContFailDateEnd())) {
criteria.and("contFailDate").gte(dto.getContFailDateStart()).lt(dto.getContFailDateEnd());
}
return criteria;
}