[英]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. Mongo DB的C ++驱动程序在update()上返回void,这与客户端返回写结果以指示要更新的文档数量的客户端不同。 From my understanding, an update operation that affects 0 documents is a perfectly legal result hence no Exceptions are thrown. 据我了解,影响0个文档的更新操作是完全合法的结果,因此不会引发任何异常。
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. 我之所以这样问,是因为我正在通过数据库执行Upsert,并且我想知道Upsert是创建新文档还是更新数据库。 Without a write result, I am not able to efficiently determine the outcome of the upsert. 没有写结果,我将无法有效地确定upsert的结果。
1) Is there a reason why no return is provided for the c++ driver 1)有没有为什么没有为C ++驱动程序提供退货的原因
2) In this event, is there a proper way to retrieve the write result without having to perform a query on the DB. 2)在这种情况下,是否有一种适当的方法可以检索写入结果,而不必在数据库上执行查询。
getLastErrorDetailed()
seems to be the answer: getLastErrorDetailed()
似乎是答案:
(*conn)->update(...);
BSONObj obj = (*conn)->getLastErrorDetailed();
const string err_msg = (*conn)->getLastErrorString(obj);
int n = obj.getIntField("n");
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); getLastError()只在新的旧版(legacy_1.0.0-rc0)c ++驱动程序中返回错误字符串: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. 我看不到如何检索写入结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.