MongoKitten: Insert of multiple documents with errors
The insert method won’t return any object ids, if a duplicate key error happens, but it throws an error.
This behaviour would be acceptable if the stoppingOnError parameter is true, but this parameter suggests that if the insertion contains a “bad object”, the work continues, and the result outcome should be the array of the inserted identifiers.
do {
let identifiers = try collection.insert(documents, stoppingOnError: false)
}
catch {
//this is gona be called... :(
print(error)
}
Is is possible to get back the inserted identifiers after the error happens?
The ideal solution would be to alternate the insert function behaviour, and check the stoppingOnError property & handle the outcome according to it. Does this make any sense or am I missing something?
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 20 (11 by maintainers)
Yaaaay, it’s perfect! Works like magic! Thank you for the “fix”! 🥇 🎊 👍
I guess that’s probably the best way. 👍
https://github.com/OpenKitten/MongoKitten/blob/bson5/Tests/MongoKittenTests/CollectionTests.swift#L601
Here is the test, do you think this is enough?
Sounds good. I’m thinking of making it a bit different even.
struct InsertFailurewhich has helper methods, too, to get information about the successful and failed documents. I’ll be working on this later this weekend.Thanks for the issue. I’ve been thinking about this for a bit now, but I don’t think it’s easy to solve. I can, however, modify the error signature to contain the inserted ids. Would that help?
In this case you could do something similar to this: