MongoDB相关使用问题

1.【报错】sort operation used more than the maximum 33554432 bytes of RAM. Add an index

MongoDB 排序超过内存限制,限制最大为100M。

解决方式:将内存排序改为磁盘排序

正常用法:数据量大了再排序会报错

java 复制代码
@Autowired
protected MongoOperations mongoTemplate;

public List<Student> getStudent(Long cid, Integer pageNo, Integer pageSize){
	Sort sort = new Sort(new Sort.Order(Sort.Direction.DESC,"createTime"));
	Query query = Query.query(Criteria.where("cid").is(cid)).with(sort);
	if (pageNo != null && pageSize != null) {
	   query.with(PageRequest.of(pageNo - 1, pageSize));
	}
	List<Student> students = mongoTemplate.find(query, Student.class).stream().limit(100).collect(Collectors.toList());
}

优化方法:采用磁盘查询

java 复制代码
import com.iqiyi.student.entity.mongo.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.*;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class StudentMongoDao{

    @Autowired
    private MongoTemplate mongoTemplateOrigin;
    protected Class<T> entityClass;

    public List<Student> getStudent(Long cid, Integer pageNo, Integer pageSize){
        MatchOperation matchOperation = Aggregation.match(Criteria.where("cid").is(cid));
        SortOperation sortOperation = Aggregation.sort(Sort.by(Sort.Direction.DESC, "createTime"));
        Aggregation aggregation = Aggregation.newAggregation(matchOperation, sortOperation);

		// 如果需要分页
		if (pageNo != null && pageSize != null) {
            Pageable pageable = PageRequest.of(pageNo - 1, pageSize);
            SkipOperation skipOperation = Aggregation.skip((long) pageable.getPageNumber() * pageable.getPageSize());
            LimitOperation limitOperation = Aggregation.limit(pageable.getPageSize());
            aggregation = Aggregation.newAggregation(matchOperation, sortOperation, skipOperation, limitOperation);
        }
		
		// 查询
        Aggregation finalAggregation = aggregation.withOptions(Aggregation.newAggregationOptions().allowDiskUse(true).build());
        List<Student> students= mongoTemplateOrigin.aggregate(finalAggregation, entityClass.getAnnotation(Document.class).collection(), entityClass).getMappedResults();
        return students;
    }
}

2.【报错】Map key xxx.xxx contains dots but no replacement was configured

原因:mongoDb有自己的内容解析方式,不支持内容中出现"."(英文点号)。

解决:将该符号替换掉即可

update 2025.1.3

相关推荐
zhanghaha131428 分钟前
Python语言基础:4_数据类型转换
java·前端·python
Devin~Y1 小时前
互联网大厂Java面试实战:Spring Boot、MyBatis、Redis、Kafka、JWT、Spring Cloud 与 AI 场景追问
java·spring boot·redis·spring cloud·kafka·mybatis·spring security
花生了什么事o1 小时前
DDD:领域驱动设计的初步认识
java·数据库
BGK1123582 小时前
基于qemu_v8+optee 4.00 平台构建 ca/ta
java·大数据·数据库
何中应2 小时前
Spring Boot整合Doris
java·数据库·spring boot
笨笨饿2 小时前
101_详解USB协议
java·jvm·数据结构
J-Tony112 小时前
【Redis】数据结构&&持久化
数据结构·数据库·redis
遨游DATA3 小时前
Maven dependencyManagement 已声明却仍缺 Jar:如何验证最终运行包
java·spring boot·maven·故障排查·依赖管理
腻害兔3 小时前
【若依项目-产品经理视角】深度拆解 RuoYi-Vue-Pro 认证与权限:RBAC + 数据权限,这套“门禁系统“到底怎么设计的?
java·vue.js·人工智能·产品经理·ai编程
KaMeidebaby3 小时前
卡梅德生物技术快报|原核膜蛋白表达优化实操手册,膜蛋白的纯化梯度洗脱完整流程
前端·网络·数据库·人工智能·算法