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 ...
随机推荐
- 分布式系统/NoSQL
分布式系统概念与架构:https://www.cnblogs.com/JonaLin/category/1555338.html 分布式缓存 redis系列:https://blog.csdn.net ...
- gitlab不能启动了
gitlab意外停止后不能启动,执行gitlab-ctl start 提示全部启动失败. GitLab won’t start – runsv not running. Gitlab didn’t s ...
- Java实现二叉树地遍历、求深度和叶子结点的个数
一.分析 二叉树是n个结点所构成的集合,它或为空树,或为非空树.对于非空树,它有且仅有一个根结点,且除根结点以外的其余结点分为两个互不相交的子集,分别称为左子树和右子树,它们本身又都是二叉树. 显而易 ...
- 减2或减3(很搞的贪心)2019牛客国庆集训派对day6
题意:https://ac.nowcoder.com/acm/contest/1111/D 问你先减二x次的情况下,最少减几次3. 思路: %3不为0的要先减2,然后%3为0的要先减大的(比如9 3 ...
- LeetCode 203——移除链表(JAVA)
删除链表中等于给定值 val 的所有节点. 示例: 输入: 1->2->6->3->4->5->6, val = 6 输出: 1->2->3->4 ...
- thymeleaf 模板使用 之 前台界面获取后台属性值
使用Thymeleaf模板时,如果需要在js中获取后台传值,那么需要用内联JS写法获取 [姿势很重要] 一.后台通过Model的addAttribute方法向前台传值 1.js获取后台属性值(--内联 ...
- C# Reflection exception Method not found
C# Reflection exception Method not found Ok I figured it out. The server has Microsft .NET 4.0 insta ...
- EasyUI_DataGrid数据操作
1.html: <div style="width: 1100px;height: 350px ;overflow: scroll"> <table id=&qu ...
- redis集群+JedisCluster+lua脚本实现分布式锁(转)
https://blog.csdn.net/qq_20597727/article/details/85235602 在这片文章中,使用Jedis clien进行lua脚本的相关操作,同时也使用一部分 ...
- ASE19团队项目alpha阶段model组 scrum2 记录
本次会议于11月4日,19时整在微软北京西二号楼sky garden召开,持续25分钟. 与会人员:Jiyan He, Kun Yan, Lei Chai, Linfeng Qi, Xueqing W ...