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

相关推荐
好学且牛逼的马6 分钟前
【Java编程思想|15-泛型】
java·windows·python
腾讯云数据库6 分钟前
「腾讯云NoSQL」技术之 MongoDB 篇:MongoDB 存储引擎备份性能70%提升内幕揭秘
数据库·nosql
日月星辰Ace10 分钟前
JDK 工具学习系列(五):深入理解 javap、字节码与常量池
java·jvm
悟空码字15 分钟前
Spring Boot 整合 Elasticsearch 及实战应用
java·后端·elasticsearch
sino爱学习18 分钟前
Guava 常用工具包完全指南
java·后端
正在走向自律18 分钟前
金仓数据库引领新能源行业数字化转型:案例深度解析与领导力展现
数据库·国产数据库·kingbasees·金仓数据库·电科金仓·新能源行业
雨中飘荡的记忆19 分钟前
Spring动态代理详解
java·spring
若水不如远方32 分钟前
深入理解Reactor:从单线程到主从模式演进之路
java·架构
爱分享的鱼鱼34 分钟前
Java高级查询、分页、排序
java
某空_44 分钟前
【Android】线程池解析
java