0

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)

2 Answers 2

1

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.

The additional benefit is that you can get the number of inserted documents from the BulkWriteResult object (returned from the execute method).

You can take a look at the Java documentation for bulk inserts here.

Edit: Just to be clear, I don't recommend that you ignore the errors, you should fix your data/inserts.

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));
Sign up to request clarification or add additional context in comments.

3 Comments

thanks i'll try that... the order doesnt matter, could you say if my own answer is correct too?
mh the BulkWriteOperation failed with the same exception, i'm trying it with the unacknowledged writes again, since i know how many objects it sould be in the end
I can confirm that an UnorderedBulkOp ignores errors and continues to do the remaining operations in mongodb 4.4. .execute() will just an error in node that can be catched, while all other of my insert operations were completed. Using an OrderedBulkOp the operations were stopped after the first error.
0

What worked for me:

wayCollection.insert(ways, new WriteConcern(0, 0, false, false, true));

3 Comments

This will "work". But by doing inserts this way you have no way of knowing if MongoDB actually inserted anything.
will it be faster slower or equal execute than the bulk method?
The unacknowledged writes are pretty fast, but I never compared the speed between bulk inserts and unacknowledged writes, so I can't really tell you for sure.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.