If the write operation fails, how do I detect the failure? Does the driver's 'remove'call throw an exception?
Yes, you are right. If you are using WriteConcern.SAFE
(and above), the driver internally examines the getLastError and throws an exception if the result document contains err. So in that case you don't have to check WriteResult
. This is also true for String based write concern such as "majority".
For "lower" WriteConcern
(NORMAL or NONE) the drivers just returns a WriteResult
and doesn't throw an exception. You will have to manually inspect it's error property. If everything went well it's null, otherwise an error has occurred.