我正在尝试进行无序批量插入,但是我收到了写入错误,并且在同一个插件上工作。
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);
这是log:
Bulk write operation error on server IP:27017. Write errors: [com.mongodb.BulkWriteError@24cd7085].
com.mongodb.BulkWriteException: Bulk write operation error on server 192.168.50.166:27017. Write errors: [com.mongodb.BulkWriteError@24cd7085].
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)
我使用的是Mongo 3.0.4 Java Driver。
答案 0 :(得分: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);