MongoDB学习笔记-解析jsonCommand内容

embedded/2025/2/6 23:51:27/
如果需要屏蔽其他项目对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/embedded/160148.html

相关文章

在Debian 12上安装VNC服务器

不知道什么标题 可以看到这个文章是通过豆包从国外网站copy的&#xff0c;先这样写着好了&#xff0c;具体的我有时间再补充&#xff0c;基本内容都在这里了。 在Debian 12上安装VNC服务器 简介 VNC&#xff08;Virtual Network Computing&#xff0c;虚拟网络计算&#xf…

Unity开发游戏使用XLua的基础

Unity使用Xlua的常用编码方式&#xff0c;做一下记录 1、C#调用lua 1、Lua解析器 private LuaEnv env new LuaEnv();//保持它的唯一性void Start(){env.DoString("print(你好lua)");//env.DoString("require(Main)"); 默认在resources文件夹下面//帮助…

Ubuntu部署Deepseek-R1模型(8b)

安装ubuntu系统 本机电脑系统ubuntu-20.04 #升级软件 sudo apt-get update#安装curl sudo apt-get install curl通过以上两条指令&#xff0c;完成了curl命令的安装。 安装ollama 打开Ollama官网 选择Linux&#xff0c; 给出如上图方框所示的一条指令 curl -fsSL https:…

OpenCV:图像轮廓

目录 简述 1. 什么是图像轮廓&#xff1f; 2. 查找图像轮廓 2.1 接口定义 2.2 参数说明 2.3 代码示例 2.4 运行结果 3. 绘制图像轮廓 3.1 接口定义 3.2 参数说明 3.3 代码示例 3.4 运行结果 4. 计算轮廓周长 5. 计算轮廓面积 6. 示例&#xff1a;计算图像轮廓的面…

中国城商行信贷业务数仓建设白皮书(第一期:总体规划)

一、项目背景与行业现状 1.1 国内城商行信贷业务痛点 2024年统计数据显示:全国134家城商行平均历史数据处理延迟达37小时/次 传统Oracle架构日均处理能力上限仅为320万笔交易 客户特征维度不足(现行系统平均维护86个客户标签) 监管报表生成耗时超同业股份制银行2.3倍 1.2 H…

车载软件架构 --- 基于AUTOSAR软件架构的ECU开发流程小白篇

我是穿拖鞋的汉子&#xff0c;魔都中坚持长期主义的汽车电子工程师。 老规矩&#xff0c;分享一段喜欢的文字&#xff0c;避免自己成为高知识低文化的工程师&#xff1a; 简单&#xff0c;单纯&#xff0c;喜欢独处&#xff0c;独来独往&#xff0c;不易合同频过着接地气的生活…

Java进阶文件输入输出实操(图片拷贝)

Java进阶文件输入输出实操&#xff08;图片拷贝&#xff09; 把某个目录下的全部图片&#xff0c;全部拷贝到另外一个目录 package test; import domee.chapter6_7.B; import java.io.*; public class Ex10_10 { public static void main(String[] args) throws IOException { …

用BGP的路由聚合功能聚合大陆路由,效果显著不?

正文共&#xff1a;666 字 11 图&#xff0c;预估阅读时间&#xff1a;1 分钟 之前我们统计过中国境内的IP地址和路由信息&#xff08;你知道中国大陆一共有多少IPv4地址吗&#xff1f;&#xff09;&#xff0c;不过数量比较多&#xff0c;有8000多条。截止到2021年底&#xff…