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
mongodb 批量添加、修改和删除的更多相关文章
- ASP.NET MVC用存储过程批量添加修改数据
用Entity Framework 进行数据库交互,在代码里直接用lamda表达式和linq对数据库操作,中间为程序员省去了数据库访问的代码时间,程序员直接可以专注业务逻辑层的编写.但是对于比较复杂的 ...
- EF 批量 添加 修改 删除
1批量添加 db.T_Investigator.AddRange(list) 2批量删除 db.T_Investigator.RemoveRange(list) 3批量修改 for 循 ...
- DNS添加/修改/查询/删除A记录
#查询DNS可用类 Get-WmiObject -Namespace root\MicrosoftDNS -List #查询所有资源记录 $mydns = [WMIClass]"ROOT\M ...
- Zabbix 4.0 API 实践,主机/主机群组 批量添加模板和删除模板
场景 我们日常在管理Zabbix 的时候,经常会需要批量添加模板和批量删除模板,Zabbix页面是提供的批量链接的功能,但是它链接的也只是当前页的主机,我们想扩展这个功能,在链接的时候,可以批量链接整 ...
- 基于JQuery easyui,gson的批量新增/修改和删除-servlet版
最近项目需要用到在页面进行批量操作,做了一些这方面的学习,参照网上的资料写了个小例子,记录一下: 准备 引入gson-2.6.2.jar,这里使用gson而不使用json-lib,原因是json-li ...
- Entity Framework Code First添加修改及删除单独实体
对于一个单独实体的通常操作有3种:添加新的实体.修改实体以及删除实体. 1.添加新的实体 Entity Framework Code First添加新的实体通过调用DbSet.Add()方法来实现. ...
- Entity Framework Code First添加修改及删除外键关联实体
1.添加外键关联实体 1>.添加新的Province及City实体 using (var ctx = new PortalContext()) { var city1 = new City { ...
- SqlSugar批量添加修改问题
直接InsertRange空集合会报错,如果我们是同时执行多个添加或修改,不要共用一个上下文,最好是在方法里面声明上下文进行区分,不然容易报错 //如果同时执行多个添加,更新 操作不要共用一个上下文, ...
- react.js 之 批量添加与删除功能
最近做的CMS需要用到批量添加图片的功能:在添加文件的容器盒子内,有两个内容,分别是:添加按钮与被添加的选择文件组件. 结构分析: 被添加的组件,我们称为:UploadQiNiuFiles(七牛文件上 ...
随机推荐
- 小记--------sqoop的简单从mysql导入到hbase操作
sqoop import -D sqoop.hbase.add.row.key=true //是否将rowkey相关字段列入列族中,默认为false :该 ...
- PAT A1001 A+B Format (20 分)
AC代码 #include <cstdio> #include <algorithm> using namespace std; const int maxn = 11; in ...
- HIVE udf实例
本例中udf来自<hive编程指南>其中13章自定义函数中一个例子. 按照步骤,第一步,建立一个项目,创建 GenericUDFNvl 类. /** * 不能接受第一个参数为null的情况 ...
- llinux 进阶篇
df 查看磁盘空间 df -h (加了之后就有单位了) free 指令,查看内存使用情况 free -m(表示一mb单位进行查看) head 查看一个文件的前n行,如果没有n 就默认为前10行 hea ...
- python *****【异常处理】*****
try: val = input('请输入数字:') num = int(val) except Exception as e: print('操作异常') # import requests # # ...
- Boot-crm管理系统开发教程(总结)
这个Boot-crm管理系统我花了大概两周写完,因为是刚学完SSM框架,所以立马开始了这个项目,项目初期,运行书本上给的前端代码都报了许多错误,导致这个原因是因为书本给的 设计说明文档 没有看清楚.然 ...
- 并不对劲的bzoj4538:loj2049:p3250:[HNOI2016]网络
题意 有一棵\(n\)(\(n\leq 10^5\))个点的树,\(m\)(\(m\leq 2\times 10^5\))个操作.操作有三种:1.给出\(u,v,k\),表示加入一条从\(u\)到\( ...
- Rust 优劣势: v.s. C++ / v.s. Go(持续更新)
Rust 发展速度比 C++ 强很多.如果去翻 open-std 的故纸堆,会发现 C++ 这边有很多人(包括标准委员会的人)提了有用的提案,但后来大多不了了之或经历了非常长的时间才进入标准. > ...
- docker-compose.yml 部署Nginx、Java项目、MySQL、Redis
version: "3.7" services: nginx: image: nginx restart: always container_name: nginx environ ...
- 【原创】大叔问题定位分享(33)oozie提交任务报错ArithmeticException: / by zero
oozie提交workflow后执行task报错: 2019-07-04 17:19:00,559 ERROR [RMCommunicator Allocator] org.apache.hadoop ...