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 批量添加、修改和删除的更多相关文章

  1. ASP.NET MVC用存储过程批量添加修改数据

    用Entity Framework 进行数据库交互,在代码里直接用lamda表达式和linq对数据库操作,中间为程序员省去了数据库访问的代码时间,程序员直接可以专注业务逻辑层的编写.但是对于比较复杂的 ...

  2. EF 批量 添加 修改 删除

    1批量添加    db.T_Investigator.AddRange(list) 2批量删除    db.T_Investigator.RemoveRange(list) 3批量修改   for 循 ...

  3. DNS添加/修改/查询/删除A记录

    #查询DNS可用类 Get-WmiObject -Namespace root\MicrosoftDNS -List #查询所有资源记录 $mydns = [WMIClass]"ROOT\M ...

  4. Zabbix 4.0 API 实践,主机/主机群组 批量添加模板和删除模板

    场景 我们日常在管理Zabbix 的时候,经常会需要批量添加模板和批量删除模板,Zabbix页面是提供的批量链接的功能,但是它链接的也只是当前页的主机,我们想扩展这个功能,在链接的时候,可以批量链接整 ...

  5. 基于JQuery easyui,gson的批量新增/修改和删除-servlet版

    最近项目需要用到在页面进行批量操作,做了一些这方面的学习,参照网上的资料写了个小例子,记录一下: 准备 引入gson-2.6.2.jar,这里使用gson而不使用json-lib,原因是json-li ...

  6. Entity Framework Code First添加修改及删除单独实体

    对于一个单独实体的通常操作有3种:添加新的实体.修改实体以及删除实体. 1.添加新的实体 Entity Framework Code First添加新的实体通过调用DbSet.Add()方法来实现. ...

  7. Entity Framework Code First添加修改及删除外键关联实体

    1.添加外键关联实体 1>.添加新的Province及City实体 using (var ctx = new PortalContext()) { var city1 = new City { ...

  8. SqlSugar批量添加修改问题

    直接InsertRange空集合会报错,如果我们是同时执行多个添加或修改,不要共用一个上下文,最好是在方法里面声明上下文进行区分,不然容易报错 //如果同时执行多个添加,更新 操作不要共用一个上下文, ...

  9. react.js 之 批量添加与删除功能

    最近做的CMS需要用到批量添加图片的功能:在添加文件的容器盒子内,有两个内容,分别是:添加按钮与被添加的选择文件组件. 结构分析: 被添加的组件,我们称为:UploadQiNiuFiles(七牛文件上 ...

随机推荐

  1. python列表的切片与复制

    切片,即处理一个完整列表中部分数据. 语法 变量[起始索引:终止索引:步长] 首先创建一个字符串列表 >>> cars = ['toyota', 'honda', 'mazda', ...

  2. Linux 防火墙设置常用指令

    查看防火墙状态命令: service firewalld status systemctl status firewalld 结果: 其中:   enabled:开机启动(开机不启动是disabled ...

  3. 【LOJ】#3032. 「JOISC 2019 Day1」馕

    LOJ#3032. 「JOISC 2019 Day1」馕 处理出每个人把馕切成N段,每一段快乐度相同,我们选择第一个排在最前的人分给他的第一段,然后再在未选取的的人中选一个第二个排在最前的切一下,并把 ...

  4. Codeforces Educational Codeforces Round 3 E. Minimum spanning tree for each edge 树上倍增

    E. Minimum spanning tree for each edge 题目连接: http://www.codeforces.com/contest/609/problem/E Descrip ...

  5. python模块-paramiko-修改源码(demo实例)

    在github上下载一个paramiko,找到demos目录,复制到pycharm项目里面 这是因为python3传来得数据是bates数据类型,需要decode()一下就可以,在demos下得int ...

  6. python设计购物车

    设计购物车 一需求: 1.启动程序后,输入用户名密码后,如果是第一次登录,让用户输入工资,然后打印商品列表 2.允许用户根据商品编号购买商品 3.用户选择商品后,检测余额是否够,够就直接扣款,不够就提 ...

  7. Gluster的搭建和使用

    Gluster的搭建和使用 序言 我们为什么要去使用分布式存储,在一家大型公司或者大规模的集群中,大家可能会经常遇到一个问题,我的数据怎么存放,放在那,数据空间不够了怎么办,这些问题经常困扰着我们. ...

  8. jq之display:none与visible:hidden

    http://www.cnblogs.com/linxiong945/p/4075146.html 今天学习到jquery的hide()部分时,突然有一个想法,jquery中的隐藏/显示部分的实现是给 ...

  9. arcgisJs之featureLayer中feature的获取

    arcgisJs之featureLayer中feature的获取 在featureLayer中source可以获取到一个Graphic数组,但是这个数组属于原数据数组.当使用 applyEdits修改 ...

  10. JavaScript的常用浏览器设置

    用什么浏览器?如果您不告诉我您使用的浏览器,我将告诉您有关JavaScript的常用浏览器设置.~火狐在菜单栏中选择工具->选项->内容以查看启用javascript的选项.Interne ...