MongoDB学习笔记-解析jsonCommand内容

ops/2025/2/6 14:06:49/
如果需要屏蔽其他项目对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/ops/156169.html

相关文章

使用MATLAB进行雷达数据采集可视化

本文使用轮趣科技N10雷达&#xff0c;需要源码可在后台私信或者资源自取 1. 项目概述 本项目旨在通过 MATLAB 读取 N10 激光雷达 的数据&#xff0c;并进行 实时 3D 点云可视化。数据通过 串口 传输&#xff0c;并经过解析后转换为 三维坐标点&#xff0c;最终使用 pcplayer 进…

【Linux系统】—— make/makefile

【Linux系统】—— make/makefile 1 什么是 make/makefile2 第一版本makefile3 依赖关系和依赖方法4 清理4.1 清理的基本语法4.2 make 的默认执行4.3 为什么要加 『.PHONY:clean』4.3.1 『.PHONY:clean』的功能4.3.2 如何理解总是不被执行4.3.2 如何区分文件的新旧 5 第二版本m…

C语言的物联网

C语言在物联网中的应用 物联网&#xff08;Internet of Things&#xff0c;IoT&#xff09;是一个通过网络将各种物理设备连接起来的系统&#xff0c;使其能够收集和交换数据。随着技术的进步&#xff0c;物联网已经走入了我们的日常生活&#xff0c;并在智能家居、智能城市、…

设计模式学习(二)

结构型 适配器模式 定义 它允许将一个类的接口转换成客户端期望的另一个接口。适配器模式通常用于使不兼容的接口能够一起工作。 适配器模式的角色 目标接口&#xff08;Target&#xff09;&#xff1a;客户端期望的接口。适配者&#xff08;Adaptee&#xff09;&#xff…

excel里面的数据怎样批量地处理,把数据竖排便成横排?

废话不多说&#xff0c;直接说步骤吧&#xff01;先看效果&#xff1a; 原本的数据&#xff1a; 转置后的数据&#xff1a; 步骤一&#xff1a;选择需要转置的数据 选中你想要转置的数据区域。这些数据可以是单列、多列&#xff0c;甚至包含多个数据点的区域。例如&#xff0…

从Transformer到世界模型:AGI核心架构演进

文章目录 引言&#xff1a;架构革命推动AGI进化一、Transformer&#xff1a;重新定义序列建模1.1 注意力机制的革命性突破1.2 从NLP到跨模态演进1.3 规模扩展的黄金定律 二、通向世界模型的关键跃迁2.1 从语言模型到认知架构2.2 世界模型的核心特征2.3 混合架构的突破 三、构建…

数据建模简介

1. 数据建模 数据建模是将现实世界的业务需求转换为结构化的数据模型的过程&#xff0c;目的是为数据存储&#xff0c;管理和分析提供蓝图。以下是数据建模的核心步骤和关键要点&#xff1a; 1. 需求分析(明确目标) 确定业务目标 与业务方深入沟通&#xff0c;明确模型用途(…

前端导出Excel表格

1.主要依赖 xlsxfile-saver 2.核心代码 import * as XLSX from xlsx; import FileSaver from "file-saver"; // 导出excel表格 const exportExcel (table, name null) > {console.log("获取表格", unref(table))try {let tableDom unref(table).$e…