Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB: What does getLastError() return

Tags:

java

mongodb

Thought the WriteResult.getLastError() should return null, if the delete
operation was successful.

It returns this

{ "n" : 1 , "connectionId" : 200 , "wtime" : 0 , "err" :  null  , "ok" : 1.0}  

The BatchData Document was deleted successfully, but getLastError() is not null.

How should I write the code to know, if the delete was unsuccessful, in the following snippet:

try {   
  Query<BatchData> queryDeleteBatchData = mongo.createQuery(BatchData.class);   
  queryDeleteBatchData.field("uuid").equal(theBatch.uuid);    
  queryDeleteBatchData.field("senderUuid").equal(on.uuid);   

  WriteResult del = mongo.delete(queryDeleteBatchData);   

  if(del.getLastError() != null){    
     logger.error("ERROR");  
  }   

} catch (Exception e) {
  logger.error("ERROR" );
}
like image 644
Erik Avatar asked Oct 20 '11 17:10

Erik


1 Answers

The getLastError() command is doing the correct thing. It's telling you that the action was successful (ok:1.0) and that no error occurred ("err":null).

For more details check out the recently updated docs.

getLastError() also has some functionality related to journaling and replication that you may want to investigate.


Edit:

In response to the first comment:

...
  if(del.getLastError().ok != 1.0){    
     logger.error("ERROR");  
  }   

} catch (Exception e) {
  logger.error("ERROR" );
}
like image 133
Gates VP Avatar answered Sep 21 '22 20:09

Gates VP