mongodb 批量添加、修改和删除
1.使用MongoTemplate
a.批量插入
Insert a Collection of objects into a collection in a single batch write to the database.
<T> Collection<T> insert(Collection<? extends T> batchToSave, Class<?> entityClass);
或Insert a batch of objects into the specified collection in a single batch write to the database.
<T> Collection<T> insert(Collection<? extends T> batchToSave, String collectionName);
或 Insert a mixed Collection of objects into a database collection determining the collection name to use based on the
class.(将混合的对象集合插入数据库集合中,根据类确定要使用的集合名称。)
<T> Collection<T> insertAll(Collection<? extends T> objectsToSave);
b.批量更新
Updates all objects that are found in the collection for the entity class that matches the query document criteria
with the provided updated document.
UpdateResult updateMulti(Query query, Update update, Class<?> entityClass);
或
Updates all objects that are found in the specified collection that matches the query document criteria with the
provided updated document.
UpdateResult updateMulti(Query query, Update update, String collectionName);
或
Updates all objects that are found in the collection for the entity class that matches the query document criteria
with the provided updated document.
UpdateResult updateMulti(Query query, Update update, Class<?> entityClass, String collectionName);
c.批量删除
Remove all documents that match the provided query document criteria from the the collection used to store the
entityClass. The Class parameter is also used to help convert the Id of the object if it is present in the query.
DeleteResult remove(Query query, Class<?> entityClass);
DeleteResult remove(Query query, Class<?> entityClass, String collectionName);
DeleteResult remove(Query query, String collectionName);
d.查找并删除
<T> List<T> findAllAndRemove(Query query, String collectionName);
<T> List<T> findAllAndRemove(Query query, Class<T> entityClass);
<T> List<T> findAllAndRemove(Query query, Class<T> entityClass, String collectionName);
2.还有其他批量操作的方式。例如基于BulkOperations的操作
使用形式:
例:BulkOperations bulkOp = this.template.bulkOps(BulkMode.UNORDERED, COLLECTION_NAME);
获取BulkOperations对象后,在进行批量操作,具体查看BulkOperations提供的方法。
最后,execute()执行。
public Integer batchInsertDetailCommonDailyMessages(List<DetailCommonDailyStatis> messages) { if (CollectionUtils.isEmpty(messages)) { return 0; } // 插入新数据 BulkOperations bulkOp = this.template.bulkOps(BulkMode.UNORDERED, COLLECTION_NAME); //BulkNode有两种形式 bulkOp.insert(messages); BulkWriteResult result = bulkOp.execute();int insertCount = result.getInsertedCount();return insertCount; }
3.基于MongoRepository
原文地址:https://www.cnblogs.com/muxi0407/p/11765140.html
- Python之dict(或对象)与json之间的互相转化
- datetime.date(2014, 4, 25) is not JSON serializable
- Linux下PAM模块学习总结
- 统计文件中出现的单词次数
- EF批量操作数据与缓存扩展框架
- 前端面试题:JS中的let和var的区别
- 虚拟机字节码执行引擎
- 快速傅里叶变换(FFT)详解
- SQLServer 数据库镜像+复制切换方案
- Handler源码分析
- STM32-对芯片启动读保护,实现加密(详解)
- Django+xadmin打造在线教育平台(一)
- “无法删除数据库,因为该数据库当前正在使用”问题解决
- HDU 4372 Count the Buildings
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 《剑指offer》第26天:最大子序和
- Threat Dragon:一款针对OWASP的威胁模型构建平台
- 《剑指offer》第25天:最简单的动态规划
- 基于web页面开发串口程序界面---html代码
- Debotnet:一款针对Windows10隐私设置和数据的保护工具
- 基于web页面开发串口程序界面---代码实现
- 《剑指offer》第24天:链表相加
- 《剑指offer》第23天:删除链表倒数第N个节点
- 《剑指offer》第22天:链表成环的新解法
- 一文入门Android逆向
- 安全研究 | Slack桌面应用程序的RCE漏洞+XSS漏洞
- Softmax和Cross-entropy是什么关系?
- PyTorch多GPU并行训练方法及问题整理
- ggplot2在图上添加线性拟合方程和R值
- JTAG