MongoDB学习笔记-解析jsonCommand内容

news/2025/2/6 18:28:12/
如果需要屏蔽其他项目对MongoDB的直接访问操作,统一由一个入口访问操作MongoDB,可以考虑直接传入jsonCommand语句解析执行。
  • 相关依赖包
<!-- SpringBootDataMongodb依赖包 -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-mongodb</artifactId><version>2.4.2</version>
</dependency>
  • 部分代码
java">@Resourceprotected MongoProperties mongoProperties;public List<Map<String, Object>> readList(String mongoTemplateName, String collectionName, String jsonCommand)throws BusinessException {ParamUtils.checkParams(mongoTemplateName, collectionName, jsonCommand);List<Document> documentList = executeCommand(mongoTemplateName, collectionName, jsonCommand);if (null == documentList || documentList.isEmpty()) {return new ArrayList<>();}List<Map<String, Object>> resultList = new ArrayList<>();for (Document currentDocument : documentList) {Map<String, Object> result = new HashMap<>();for (Map.Entry<String, Object> entry : currentDocument.entrySet()) {result.put(entry.getKey(), entry.getValue());}resultList.add(result);}return resultList;}private List<Document> executeCommand(String mongoTemplateName, String collectionName, String jsonCommand) {return mongoTemplateCache.get(mongoTemplateName).withSession(ClientSessionOptions.builder().build()).execute(session -> {long startTimeMillis = System.currentTimeMillis();List<Document> allDocuments = new ArrayList<>();Document initialDocument = session.executeCommand(jsonCommand);Document cursorDocument = initialDocument.get("cursor", Document.class);List<Document> firstBatchDocuments = cursorDocument.getList("firstBatch", Document.class);if (null != firstBatchDocuments && !firstBatchDocuments.isEmpty()) {allDocuments.addAll(firstBatchDocuments);}Long cursorId = cursorDocument.getLong("id");while (null != cursorId && cursorId != 0) {try {Document nextBatchCommand = new Document("getMore", cursorId).append("collection", collectionName).append("batchSize", mongoProperties.getDocumentBatchSize());Document nextBatchResult = session.executeCommand(nextBatchCommand);Document nextCursorDocument = nextBatchResult.get("cursor", Document.class);List<Document> nextBatchDocuments = nextCursorDocument.getList("nextBatch", Document.class);if (null != nextBatchDocuments && !nextBatchDocuments.isEmpty()) {allDocuments.addAll(nextBatchDocuments);}cursorId = nextCursorDocument.getLong("id");} catch (Exception e) {log.error(e.getMessage(), e);break;}}executionSqlLog(mongoTemplateName, collectionName, jsonCommand, startTimeMillis);return allDocuments;});}private void executionSqlLog(String mongoTemplateName, String collectionName, String jsonCommand,long startTimeMillis) {if (!mongoProperties.isExecutionSqlEnable()) {return;}long spendTime = System.currentTimeMillis() - startTimeMillis;log.info("\n==============  SQL START  ==============" +"\nExecution MON :{} {} [{} ms]" +"\nExecution SQL :{}" +"\n==============  SQL END    ==============\n", mongoTemplateName, collectionName,spendTime, jsonCommand.replaceAll("\\s{2,}", " "));}}

http://www.ppmy.cn/news/1569868.html

相关文章

【Golang学习之旅】Go 语言数据类型详解(string、slice、map等)

文章目录 前言1. Go语言数据类型概览2. Go语言基本数据类型2.1 整型&#xff08;int&#xff0c;uint&#xff0c;float&#xff09;2.2 布尔类型&#xff08;bool&#xff09;2.3 字符串&#xff08;string&#xff09; 3. Go 语言复合数据类型3.1 数组&#xff08;Array&…

CNN的各种知识点(一):卷积神经网络CNN通道数的理解!

卷积神经网络CNN通道数的理解&#xff01; 通道数的核心概念解析1. 通道数的本质 2. 单张灰度图的处理示例&#xff1a; 3. 批量输入的处理通道与批次的关系&#xff1a; 4. RGB三通道输入的处理计算过程&#xff1a;示例&#xff1a; 5. 通道数的实际意义6. 可视化理解(1) 单通…

HTML基本语法

什么是HTML? HTML是超文本标记语言&#xff08;HyperText Markup Language&#xff09;的缩写&#xff0c;是一种用于创建网页的标准标记语言。HTML允许网页设计师通过使用标签来描述网页的结构和内容。 W3C标准 W3C&#xff08;World Wide Web Consortium&#xff09;是一…

11.kafka开启jmx

方式一: 1.进入/opt/kafka_2.13-3.3.2/bin目录 命令: cd /opt/kafka_2.13-3.3.2/bin [root@rhel77 ~]# cd /opt/kafka_2.13-3.3.2/bin [root@rhel77 bin]# pwd /opt/kafka_2.13-3.3.2/bin [root@rhel77 bin]# 2.备份kafka-run-class.sh 命令: cp kafka-run-class.sh …

CVPR | CNN融合注意力机制,芜湖起飞!

**标题&#xff1a;**On the Integration of Self-Attention and Convolution **论文链接&#xff1a;**https://arxiv.org/pdf/2111.14556 **代码链接&#xff1a;**https://github.com/LeapLabTHU/ACmix 创新点 1. 揭示卷积和自注意力的内在联系 文章通过重新分解卷积和自…

Linux iostat 命令使用详解

简介 iostat 命令用于监控 Linux 系统输入/输出设备的加载情况。它提供有关CPU 统计信息以及设备和分区的输入/输出统计信息。通过显示 I/O 操作如何影响系统性能&#xff0c;它对于诊断性能瓶颈&#xff08;例如磁盘或网络活动缓慢&#xff09;特别有用。 安装 Debian/Ubun…

阿里云 ubuntu22.04 中国区节点安装 Docker

下面是一份在 Ubuntu 22.04 (Jammy) 上&#xff0c;通过阿里云镜像源来安装并配置 Docker 的详细步骤示例&#xff0c;可在中国区阿里云节点使用&#xff1a; 一、卸载旧版本 (如已安装) 如果系统中已经安装了旧版 Docker (可能是 docker、docker-engine、docker.io、containe…

解决open-webui报错Couldn‘t find ffmpeg or avconv

启动open-webui的时候报错Couldnt find ffmpeg or avconv 具体内容&#xff1a; INFO [open_webui.env] Embedding model set: sentence-transformers/all-MiniLM-L6-v2 E:\py311\Lib\site-packages\pydub\utils.py:170: RuntimeWarning: Couldnt find ffmpeg or avconv - d…