首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Java-API中,MongoDB insertMany或散列抛出重复的键错误。

在Java-API中,MongoDB insertMany或散列抛出重复的键错误。
EN

Stack Overflow用户
提问于 2018-11-13 08:14:39
回答 1查看 2.3K关注 0票数 2

当我尝试使用java将许多数据插入到Mongodb4.0 (复制集)中时,它会抛出重复的关键错误。数据量不算太大,只有300000左右。并在3-5秒内插入

首先,我从官方文件和网站上搜索。它显示了

每个进程高达256^3 (16777216)

数据源来自rocketmq。这是我的密码

代码语言:javascript
复制
consumer.subscribe(GetPropertyUtils.getTestTopic(), "*", new MessageListener() {
            long insertnums = 0L ;
            List<Document> documentList= new ArrayList<Document>();
            @Override
            public Action consume(Message message, ConsumeContext context) {
                insertnums ++ ;
                consumerlogger.info(" now  total size is " + insertnums);

                String body = new String(message.getBody());
                Document document = Document.parse(body);
                documentList.add(document);
                //insert bulk 
                if(documentList.size()>=1000) {
                    try {
                        MongoInsert.insertData(documentList);
                        Thread.sleep(1000);
                    }catch (Exception e){
                        consumerlogger.error("insert sleep  3000");
                    }

                    documentList.clear();
                }
                return Action.CommitMessage;
            }

然后insertData进入MongoDB

代码语言:javascript
复制
 public  static  void  insertData(List<Document>  document){
        try{
            MongoInsertlogger.info("prepare to  insert ");
            //collection.insertMany(documents ,new InsertManyOptions().ordered(false));
            //---------

            List<WriteModel<Document>> requests = new ArrayList<WriteModel<Document>>();
            for (Document doc : document) {

                InsertOneModel<Document>  iom = new InsertOneModel<Document>(doc);
                requests.add(iom);
            }
            BulkWriteResult bulkWriteResult = collection.bulkWrite(requests,new BulkWriteOptions().ordered(false));
            System.out.println(bulkWriteResult.toString());


        }catch (Exception e){
            MongoInsertlogger.error("insert failed  , caused by " +e);
            System.out.println(e);
        }
    }

但错误显示

代码语言:javascript
复制
  BulkWriteError{index=811, code=11000, message='E11000 duplicate key error collection: yyj2.accpay index: _id_ dup key: { : ObjectId('5bea843604de38d61ff4d1fd') }', details={ }}, BulkWriteError{index=812, code=11000, message='E11000 duplicate key error collection: yyj2.accpay index: _id_ dup key: { : ObjectId('5bea843604de38d61ff4d1fe') }', details={ }}, BulkWriteError{index=813, code=11000, message='E11000 duplicate key error collection: yyj2.accpay index: _id_ dup key: { : ObjectId('5bea843604de38d61ff4d1ff') }', details={ }}, BulkWriteError{index=814, code=11000, message='E11000 duplicate key error collection: yyj2.accpay index: _id_ dup key: { : ObjectId('5bea843604de38d61ff4d200') }', details={ }}, BulkWriteError{index=815, code=11000, message='E11000 duplicate key error collection: ......

使用java中的小数据为什么会发生这种情况,对象是由MongoDB本身创建的--数据大小小于它所支持的,我使用JDBC版本mongo-java-driver3.7.1。

提前谢谢!

EN

回答 1

Stack Overflow用户

发布于 2019-08-13 17:22:43

当文档已经存在于数据库中时,您将得到此错误--由主键的副本定义。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53276552

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档