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 ...
随机推荐
- Oracle通过正则表达式分割字符串 REGEXP_SUBSTR
REGEXP_SUBSTR函数格式如下: function REGEXP_SUBSTR(string, pattern, position, occurrence, modifier) string ...
- SQL Server中bcp命令的用法以及数据批量导入导出
原文:SQL Server中bcp命令的用法以及数据批量导入导出 1.bcp命令参数解析 bcp命令有许多参数,下面给出bcp命令参数的简要解析 用法: bcp {dbtable | query} { ...
- php 正则替换特殊字符 和检测是否是中文
如果是只想输入中文的话,就这么写,要注意是分gb2312和utf-8的哦: gb2312:if(!preg_match("/^[".chr(0xa1)."-". ...
- one:arguments对象伪数组
这是我的第一个博客 <script> //计算N个数字的和 //定义一个函数,如果不确定用户是否传入了参数,或者说不知道用户传入了几个参数,没办法计算, // 但是如果在函数中知道了参数的 ...
- php双向队列的实现
队列是一种线性表,按照先进先出的原则进行 单向队列:只能从头进,从尾出 双向队列:头尾都可以进出 class DuiLie { private $array = array();//声 ...
- 3d长方体
html <div class="main"> <div class="a1">1</div> <div class= ...
- django 模块查询
# 查询轮播图slider_list = Slider.objects.filter(type = constants.SLIDER_TYPE_INDEX) # 查询新闻now_time = date ...
- django 模块创建 同步数据表 使用方法
1 配置数据库 100行左右 DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', # 'NAME': 'student ...
- 输出单项链表中倒数第k个结点——牛客刷题
题目描述: 输入一个单向链表,输出该链表中倒数第k个结点 输入.输出描述: 输入说明:1.链表结点个数 2.链表结点的值3.输入k的值 输出说明:第k个结点指针 题目分析: 假设链表长度为n,倒数第k ...
- shiro过滤器机制
shiro内置过滤器介绍 https://blog.csdn.net/qq_35608780/article/details/71703197 Shiro的Filter机制详解---源码分析 http ...