mongodb批量处理
mongodb支持批量插入。
1.使用Java mongodb api
查看源码com.mongodb.MongoCollectionImpl,有两个方法
@Override
public void insertMany(final List<? extends TDocument> documents) {
insertMany(documents, new InsertManyOptions());
} @Override
public void insertMany(final List<? extends TDocument> documents, final InsertManyOptions options) {
notNull("documents", documents);
List<InsertRequest> requests = new ArrayList<InsertRequest>(documents.size());
for (TDocument document : documents) {
if (document == null) {
throw new IllegalArgumentException("documents can not contain a null value");
}
if (getCodec() instanceof CollectibleCodec) {
document = ((CollectibleCodec<TDocument>) getCodec()).generateIdIfAbsentFromDocument(document);
}
requests.add(new InsertRequest(documentToBsonDocument(document)));
}
executor.execute(new MixedBulkWriteOperation(namespace, requests, options.isOrdered(), writeConcern)
.bypassDocumentValidation(options.getBypassDocumentValidation()));
}
insertMany(final List<? extends TDocument> documents) 默认使用 private boolean ordered = true;即有序插入。
insertMany(final List<? extends TDocument> documents, final InsertManyOptions options)调用者自己设置。 ordered属性有什么用?
/**
* Gets whether the documents should be inserted in the order provided, stopping on the first failed insertion. The default is true.
* If false, the server will attempt to insert all the documents regardless of an failures.
*
* @return whether the the documents should be inserted in order
*/
public boolean isOrdered() {
return ordered;
}
大概意思是:
true:按提供的顺序插入文档,并在首次插入失败时停止。 (按顺序插入文档,遇到失败后停止。之前已经插入成功的不返回,失败及失败之后的不插入。)
false: 服务器将尝试插入所有文档,而不考虑失败。(只要能插入成功的,都存储进数据库)
2.spring-data-mongodb
org.springframework.data.mongodb.core.MongoTemplate
以下是该方法的源码和使用案例:
/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.core.ExecutableInsertOperation#bulkOps(org.springframework.data.mongodb.core.BulkMode, java.lang.String)
*/
public BulkOperations bulkOps(BulkMode bulkMode, String collectionName) {
return bulkOps(bulkMode, null, collectionName);
}
public int batchInsertStudents() {
BulkWriteResult result = null;
try {
List<Student> documents = new ArrayList<>();
String collectionName = "myTestCol";
BulkOperations bulkOp = this.mongoTemplate.bulkOps(BulkMode.UNORDERED, collectionName);
for(int i = 502610; i< 2000000; i++) {
Student student = new Student();
student.setId(String.valueOf(i));
student.setAge(i);
student.setGender("男");
student.setName("李三"+ i);
documents.add(student);
}
bulkOp.insert(documents);
result = bulkOp.execute();
}catch (DuplicateKeyException e) {
System.out.println("**********" + e.getMessage());
}
return result;
}
有两种模式:
/**
* Mode for bulk operation.
**/
enum BulkMode { /** Perform bulk operations in sequence. The first error will cancel processing. */
ORDERED, /** Perform bulk operations in parallel. Processing will continue on errors. */
UNORDERED
};
与之前的order(true|false)相对应。
mongodb批量处理的更多相关文章
- MongoDB批量导入及简单的性能优化
今天简单分享一下MongoDB使用过程中的一些性能优化,其实并不只适用MongoDB,其他数据库多少也可适用. 首先先随机导入一千万条数据.这里我分段导入的,因为mongo的BsonDocument一 ...
- mongodb批量插入数据
年前由于公司业务需要,后台需要获取流水记录,需要每天定时跑脚本,将流水记录跑入库里边,每天大概有个一百万左右,使用的数据库是mongodb,考虑到一条一条录入数据,100多万会跑断,就想着批量录入数据 ...
- mongodb 批量更新 数组的键操作的文件
persons该文件的数据如下面的: > db.persons.find() { "_id" : 2, "name" : 2 } { "_id& ...
- mongodb批量更新操作文档的数组键
persons文档的数据如下: > db.persons.find(){ "_id" : 2, "name" : 2 }{ "_id" ...
- MongoDB批量更新和批量插入的方式
最近,在调试代码中发现向MongoDB插入或者更新文档记录时若是多条的话都是采用for循环操作的,这样的处理方式会造成数据操作耗时,不符合批量处理的原则:对此,个人整理了一下有关MongoDB的批量更 ...
- mongoDB 批量更改数据,某个字段值等于另一个字段值
由于mongodb数据库类似js的写法,所以即使数据库中新的列不存在也会自动创建 db.hospital.find().forEach( function(item){ db.hospital.upd ...
- Django+MongoDB批量插入数据
在百万级和千万级数据级别进行插入,pymongo的insert_many()方法有着很强的优势.原因是每次使用insert_one()方法进行插入数据,都是要对数据库服务器进行一次访问,而这样的访问是 ...
- 亿级别记录的mongodb批量导入Es的java代码完整实现
针对mongodb亿级别或者十亿级别的模糊查询,效率不高,解决方式是使用Es查询,这样就需要把数据导入的ES中 完整的代码实现如下所示:(仅供参考) import java.io.IOExceptio ...
- mongodb 批量添加、修改和删除
1.使用MongoTemplate a.批量插入 Insert a Collection of objects into a collection in a single batch write to ...
随机推荐
- #######【Python】【基础知识】【标准库】目录及学习规划 ######
下述参考Python DOC https://docs.python.org/zh-cn/3/library/index.html 概述 可用性注释 内置函数 内置常量 由 site 模块添加的常量 ...
- 万能数据库的使用【DbVisualizer软件,连接不同类型的数据库】
1.打开指定的软件[万能连接数据库的软件],如图所示: 2.双击“此软件”,然后会显示出该软件的主页面,如图所示: 3.点击"任一个图标",会出现如下图所示的一个弹框,意思是:创建 ...
- Web在线报表设计器使用指南
市面上的报表工具有很多,虽说功能大同小异,但每一个报表工具都有各自明确的定位,选择最合适的工具,才能达到事半功倍的效果. 本文将要介绍的ActiveReports报表工具,可全面满足 .NET 报表开 ...
- VS添加版权声明
C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\IDE\ItemTemplates\AspNetCore\ ...
- 虚拟机ubuntu连不上网
虚拟机ubuntu连不上网 解决地址:https://blog.csdn.net/spy_h/article/details/80933458
- 同一台服务器请求easyswoole的一个websocket接口报错
求助大神啊!file_get_contents报这个错:failed to open stream: Connection timed out换成curl又报这个错:couldn't connect ...
- 怎样设置 MySQL 远程连接
允许用户 root 在 任何IP 上都可以远程连接 所有 mysql数据库 并具有操作数据库的 所有权限, 密码为: myPassword mysql -u root -p grant all PRI ...
- java——ArrayList中remove()方法疑问总结
其实remove方法和contains方法大同小异,它的原理和contains方法相同https://www.cnblogs.com/lyxcode/p/9453213.html在这篇博客里面有详细说 ...
- Java开发自动售货机
1:先写一个类,包括商品的基本属性 package com.xt.java.base25; public class Goods { private int ID; private String na ...
- 服务端相关知识学习(一)之什么是zookeeper
什么是zookeeper zookeeper是分布式协调服务,可以在分布式系统中共享配置.协调锁资源.提供命名服务那分布式协调服务又是个什么东西呢?首先我们来看“协调”是什么意思.在一个并发的环境里, ...