title: Check if there is an error in update/insert | MongoDB Java driver
body: <p><strong>I want to check if an <code>insert</code> fails</strong> (due to unique=True index in the collection). If there is an error do something. Bellow is an example of my code.</p>
  
  <pre><code>DBCollection user...;
  BasicDBObject Doc = new BasicDBObject(... );
  
  String user_exists = user.insert(Doc).getError(); //insert the doc get error if any
  if(user_exists!=null){ //any errors?
      user.update(new BasicDBObject(...)); // error exists so do smthng
  }
  </code></pre>
  
  <p>The above as it is does not work. I believe that the <code>String user_exists</code> is always null. How can I make the above work?</p>
  
  <hr>
  
  <p>I have seen similar SO questions and mention the <a href="http://api.mongodb.org/java/current/com/mongodb/WriteConcern.html" rel="nofollow noreferrer">WriteConcern</a> which can be passed in the <code>insert()</code>. E.g.</p>
  
  <pre><code>coll.insert(dbObj, WriteConcern.SAFE);
  </code></pre>
  
  <p>sources: <a href="https://stackoverflow.com/a/20022811/1447885">SO question</a>
  or 
  <a href="http://docs.mongodb.org/ecosystem/drivers/java-concurrency/" rel="nofollow noreferrer">Mongo docs</a></p>
  
  <p>However I do not know which one field should I pass (SAFE, ACKNOWLEDGED, UNACKNOWLEDGED etc..) in order to get the error. Maybe I'm pointed in the wrong direction.</p>
  
  <p>I do not wish to raise an exception just to check if there is an error returned by the <code>insert</code> operation.</p>