zoukankan      html  css  js  c++  java
  • 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)相对应。

  • 相关阅读:
    金蝶中间件 前后台连不上 报跨域 前台解决方案: --user-data-dir="c:ChromeDebug" --test-type --disable-web-security
    chalk 库 console.info(chalk.blue('kkk'))
    让 js 失效 Chrome F12 右上角 settings
    openlayers.org 百度地图 静态化 同类产品
    vscode 自动格式化md文件,搞得很是郁闷,加入 [markdown] 自定义配置 "editor.formatOnSave": false 搞定了。
    electron vite2 vue3 安装 cvep my-electron-cvep
    baidu 突然打不开了 20210621
    vscode vue 鼠标Ctrl+单击 函数跳转 插件名称:vue-helper
    接口返回数据的属性值 不要返回数字0和1
    vscode 自动格式化 好使的配置 setting.json 20210622
  • 原文地址:https://www.cnblogs.com/muxi0407/p/11648383.html
Copyright © 2011-2022 走看看