2017-03-03 34 views
0

我想做無序批量插入,但我得到寫入錯誤,並在同一單插入工作。服務器IP上的批量寫入操作錯誤:27017。寫錯誤:[[email protected]]

final DBCollection col = db.getCollection(STUDENT); 
    BulkWriteOperation bulkop = col.initializeUnorderedBulkOperation(); 

    for(){ 
    //putting values in doc 
    DBObject doc = new BasicDBObject(); 
    // 
    bulkop.insert(doc); 
    } 
     bulkop.execute(WriteConcern.FSYNCED); 

這裏是日誌:

Bulk write operation error on server IP:27017. Write errors: [[email protected]]. 
com.mongodb.BulkWriteException: Bulk write operation error on server 192.168.50.166:27017. Write errors: [[email protected]]. 
at com.mongodb.BulkWriteHelper.translateBulkWriteException(BulkWriteHelper.java:57) 
at com.mongodb.DBCollection.executeBulkWriteOperation(DBCollection.java:2202) 
at com.mongodb.DBCollection.executeBulkWriteOperation(DBCollection.java:2188) 
at com.mongodb.BulkWriteOperation.execute(BulkWriteOperation.java:121) 

我使用蒙戈3.0.4 Java驅動程序。

回答

0

MongoDB的Java的3.0.1及以上版本,你可以使用這個邏輯,

 MongoClient mongoClient = new MongoClient("localhost",27017); 
     MongoDatabase database = mongoClient.getDatabase("testDB"); 

     MongoCollection<Document> collection = database.getCollection("testColl").withWriteConcern(WriteConcern.FSYNCED); 
     BulkWriteOptions options = new BulkWriteOptions(); 
     options.ordered(false); 

     ArrayList<InsertOneModel<Document>> insertList = new ArrayList<InsertOneModel<Document>>(); 
     for(int i =0; i<1000;i++){ 
      Document document= new Document().append("_id",i).append("testdat", "testvalue");; 

      insertList.add(new InsertOneModel<Document>(document)); 
     } 

     collection.bulkWrite(insertList, options); 
相關問題