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

相关推荐
RainbowSea20 小时前
12. LangChain4j + 向量数据库操作详细说明
java·langchain·ai编程
RainbowSea21 小时前
11. LangChain4j + Tools(Function Calling)的使用详细说明
java·langchain·ai编程
考虑考虑1 天前
Jpa使用union all
java·spring boot·后端
用户3721574261351 天前
Java 实现 Excel 与 TXT 文本高效互转
java
RestCloud1 天前
SQL Server到Hive:批处理ETL性能提升30%的实战经验
数据库·api
RestCloud1 天前
为什么说零代码 ETL 是未来趋势?
数据库·api
浮游本尊1 天前
Java学习第22天 - 云原生与容器化
java
ClouGence1 天前
CloudCanal + Paimon + SelectDB 从 0 到 1 构建实时湖仓
数据库
渣哥1 天前
原来 Java 里线程安全集合有这么多种
java
间彧1 天前
Spring Boot集成Spring Security完整指南
java