If you are using WriteConcern.ACKNOWLEDGED
(which I think is also SAFE
) you don't need to pollute your code with error checking.
For ACKNOWLEDGED
, the driver will automatically issue a getLastError
command automatically and raise an exception if anything got wrong, for example duplicate index violation.
Starting from v2.10 of the Java Driver, the default Write Concern is ACKNOWLEDGED
EDIT
I do not wish to raise an exception just to check if there is an error returned by the insert operation.
You shouldn't do this, but in any case:
The insert
method indeed returns WriteResult
. If it's getError()
is null
, everything is OK, otherwise it returns something such as E11000 duplicate key error index:...
. For this to work, you will have to use WriteConcern.UNACKNOWLEDGED