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

相关推荐
Mr_Xuhhh几秒前
LeetCode 热题 100 刷题笔记:从数组到字符串的经典解法(续)
java·数据结构·算法
雷工笔记2 分钟前
Navicat 备份与还原 PostgreSQL 数据库
数据库·postgresql·oracle
皙然2 分钟前
AQS模型详解:Java并发的核心同步框架(从原理到实战)
java·开发语言·jvm
愤豆5 分钟前
08-Java语言核心-JVM原理-垃圾收集详解
java·开发语言·jvm
逸Y 仙X5 分钟前
文章十四:ElasticSearch Reindex重建索引
java·大数据·数据库·elasticsearch·搜索引擎·全文检索
wregjru6 分钟前
【读书笔记】Effective C++ 条款8:别让异常逃离析构函数
java·开发语言
烤麻辣烫7 分钟前
I/O流 进阶流
java·开发语言·学习·intellij-idea
冷血~多好9 分钟前
mysql实现主从复制以及springboot实现读写分离
java·数据库·mysql·springboot
山川行12 分钟前
Python快速闯关专栏的总结
java·开发语言·笔记·python·算法·visual studio code·visual studio
两年半的个人练习生^_^13 分钟前
如何自己实现多数据源
数据库