tolma
tolma

Reputation: 141

Upate DB in Mongo DB C++ Driver

Mongo DB's C++ driver returns a void on update() unlike the client which returns a write result to indicate the number of documents being updated. From my understanding, an update operation that affects 0 documents is a perfectly legal result hence no Exceptions are thrown.

    virtual void insert( const std::string &ns, BSONObj obj , int flags=0) = 0;
    virtual void insert( const std::string &ns, const std::vector< BSONObj >& v , int flags=0) = 0;
    virtual void remove( const std::string &ns , Query query, bool justOne = 0 ) = 0;
    virtual void remove( const std::string &ns , Query query, int flags ) = 0;
    virtual void update( const std::string &ns,
                         Query query,
                         BSONObj obj,
                         bool upsert = false, bool multi = false ) = 0;
    virtual void update( const std::string &ns, Query query, BSONObj obj, int flags ) = 0;

The reason why I am asking this is because I am performing an Upsert through the DB and I would like to know if the Upsert created a new document or it updated the DB instead. Without a write result, I am not able to efficiently determine the outcome of the upsert.

1) Is there a reason why no return is provided for the c++ driver

2) In this event, is there a proper way to retrieve the write result without having to perform a query on the DB.

Upvotes: 0

Views: 447

Answers (2)

sergeh07
sergeh07

Reputation: 11

getLastErrorDetailed() seems to be the answer:

(*conn)->update(...);    
BSONObj obj = (*conn)->getLastErrorDetailed();      
const string err_msg = (*conn)->getLastErrorString(obj);    
int n = obj.getIntField("n");    

Upvotes: 1

sergeh07
sergeh07

Reputation: 11

getLastError() only returns an error string in the new legacy (legacy_1.0.0-rc0) c++ driver: std::string getLastError(bool fsync = false, bool j = false, int w = 0, int wtimeout = 0); I can't see how the write result can be retrieved.

Upvotes: 0

Related Questions