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

相关推荐
南宫生几秒前
力扣每日一题【算法学习day.133】
java·学习·算法·leetcode
獨枭3 分钟前
如何在 Mac 上安装并配置 JDK 环境变量
java·macos·jdk
m0_7383556915 分钟前
java泛型
java·开发语言
web2u19 分钟前
Docker入门及基本概念
java·运维·服务器·spring·docker·容器
CodeJourney.21 分钟前
EndNote与Word关联:科研写作的高效助力
数据库·人工智能·算法·架构
qq_2187533130 分钟前
常用Git命令
java·git
trigger3331 小时前
MongoDB 简介
数据库·mongodb
许心月1 小时前
MongoDB#常用语句
数据库·mongodb
计算机小白一个1 小时前
蓝桥杯 Java B 组之背包问题(01背包、完全背包)
java·职场和发展·蓝桥杯
计算机毕设定制辅导-无忧学长1 小时前
Maven 基础环境搭建与配置(二)
java·maven