MongoDb Java APi-忽略批量导入中的错误 [英] MongoDb Java APi - Ignore errors in bulk import
问题描述
我正在使用此功能将文档导入mongodb
Hi i'm importing Documents into mongodb with this function
WriteResult com.mongodb.DBCollection.insert(List<DBObject> list)
某些插入失败,因为数据违反索引.是否可以忽略这些错误并继续其他文档?
some inserts fail because the data violates an index. Is it possible to ignore these errors and continue with the other documents?
Exception in thread "main" com.mongodb.WriteConcernException: { "serverUsed" : "localhost:27017" , "ok" : 1 , "n" : 0 , "err" : "insertDocument :: caused by :: 16755 Can't extract geo keys from object, malformed geometry?: XXXXX , "code" : 16755}
at com.mongodb.CommandResult.getWriteException(CommandResult.java:90)
at com.mongodb.CommandResult.getException(CommandResult.java:79)
at com.mongodb.DBCollectionImpl.translateBulkWriteException(DBCollectionImpl.java:314)
at com.mongodb.DBCollectionImpl.insert(DBCollectionImpl.java:189)
推荐答案
If you're using MongoDB 2.6+ you can do an unordered bulk operation. If the error occurs when doing write operations, MongoDB will continue to process the remaining operations:
DBCollection coll = db.getCollection("test");
BulkWriteOperation bulk = coll.initializeUnorderedBulkOperation();
bulk.insert(new BasicDBObject("foo", 1));
bulk.insert(new BasicDBObject("bar", 2));
bulk.execute();
这种方法的缺点是,如果需要按顺序执行插入操作,则不能使用它,但好处是,批量插入的执行速度要比执行多个插入操作快.
The downside to this approach is that you can't use it if your inserts needs to be executed in an order, but the upside is that the bulk inserts will be executed faster than by doing multiple inserts.
另一个好处是,您可以从 BulkWriteResult
对象(从 execute
方法返回)中获得插入文档的数量.
The additional benefit is that you can get the number of inserted documents from the BulkWriteResult
object (returned from the execute
method).
您可以在此处.
修改:为了清楚起见,我不建议您忽略这些错误,而应该修复数据/插入.
Just to be clear, I don't recommend that you ignore the errors, you should fix your data/inserts.
编辑2 您还可以使用执行批量操作并为该操作设置写关注点:
Edit 2 You can also execute a bulk operation with and set a write concern for the operation:
bulk.execute(new WriteConcern(0, 0, false, false, true));
这篇关于MongoDb Java APi-忽略批量导入中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!