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. 微信小程序开放数据解密 AES-128-CBC 解密(C#版本)

    最近在开发小程序,需要跟微信服务端交互,微信敏感数据都有加密返回,需要在服务端接收进行解密后再返回给客户端小程序,今天就通过C# 进行数据的解密,官方下载下来是Node.C++.php等,就是没有C# ...

  2. 「java.util.concurrent并发包」之 ReentrantReadWriteLock

    一 引言 在多线程的环境下,对同一份数据进行读写,会涉及到线程安全的问题.比如在一个线程读取数据的时候,另外一个线程在写数据,而导致前后数据的不一致性:一个线程在写数据的时候,另一个线程也在写,同样也 ...

  3. 输出单项链表中倒数第k个结点——牛客刷题

    题目描述: 输入一个单向链表,输出该链表中倒数第k个结点 输入.输出描述: 输入说明:1.链表结点个数 2.链表结点的值3.输入k的值 输出说明:第k个结点指针 题目分析: 假设链表长度为n,倒数第k ...

  4. docker_nginx_php_mysql

    https://blog.csdn.net/miwumuge/article/details/83386679 问题 1.容器内访问宿主机地址, host.docker.internal

  5. 单元操作和仓储模式 repository+unitOfWork

    仓储和工作单元模式是用来在数据访问层和业务逻辑层之间创建一个抽象层.应用这些模式,可以帮助用来隔离你的程序在数据存储变化. 在数据源层和业务层之间增加一个repository层进行协调,有如下作用:1 ...

  6. 百度URL链接中文转码

    百度搜索链接规则为: http://www.baidu.com/s?wd=[搜索词目]&cl=3 有多个搜索词通过加号进行链接: http://www.baidu.com/s?wd=keywo ...

  7. git commit报错解决,绕过代码检查

    上一个项目用的svn,新项目用了git,很开心,终于学习了git了,本以为把git都学会了,但是还是遇到了一个不在自己学习的知识点范围内的问题,最后是同事帮忙解决的. 问题:第一次代码commit的时 ...

  8. svn经典总结

    大佬的svn:http://www.cnblogs.com/armyfai/p/3985660.html#!comments https://www.cnblogs.com/0zcl/p/730976 ...

  9. mac 下安装mysql

    1.安装mysql 使用 brew 进行安装: brew install mysql 2.安装完成: 3.如果开机启动服务 执行:brew services start mysql 否则:mysql. ...

  10. Spring的核心jar包

    Spring的主要jar包 四个核心jar包:beans.context.core.expression Spring AOP:Spring的面向切面编程,提供AOP(面向切面编程)的实现Spring ...