Java操作Mongo bulkWrite批量入库
生活随笔
收集整理的這篇文章主要介紹了
Java操作Mongo bulkWrite批量入库
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Mongo bulkWrite示例
public boolean insertBulk(String collectionName, JSONArray array) {MongoCollection<Document> collection = db.getCollection(collectionName);List<InsertOneModel<Document>> documentList = array.stream().map(item -> {Document document = Document.parse(((JSONObject)item).toJSONString());document.put("_id", document.getString("id"));return new InsertOneModel<Document>(document);}).collect(Collectors.toList());ListUtils.partition(documentList,1000).forEach(item->{collection.bulkWrite(item);});return true; }ListUtils.partition來自于
<dependency><groupId>org.apache.commons</groupId><artifactId>commons-collections4</artifactId><version>4.3</version> </dependency>總結(jié)
以上是生活随笔為你收集整理的Java操作Mongo bulkWrite批量入库的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JavaScript时间日期函数
- 下一篇: Ubuntu 安装 samba 实现文件