I'm having a hardtime figuring out what's going on with my code and how to use cmongocxx driver objects :
//...
try {
auto client = pool.acquire();
auto res = (*client)[databaseName][collectionName].bulk_write(writes, bulkWriteOption); poco_debug(*pLog.get(),
Poco::format("inserted %d / matched %d / modif %d / deleted %d / upserted %d",
res->inserted_count(), res->matched_count(), res->modified_count(),
res->deleted_count(), res->upserted_count()));
}
catch (mongocxx::bulk_write_exception &bwex) {
poco_debug_f1(*pLog.get(), "error during execute of bulkwrite:\n%s",
bsoncxx::to_json(bwex.raw_server_error().value().view()));
}
--------------------------------------------
So let's say I insert 10 documents, Exception gets triggered, I would see from the log that 3 out of 3 documents have cannot be inserted because of duplicate id
{ "nInserted" : 7, "nMatched" : 0, "nModified" : 0, "nRemoved" : 0, "nUpserted" : 0, "writeErrors" : [
{ "index" : 0, "code" : 11000, "errmsg" : "E11000 duplicate key error collection: traffic.redirects index: _id_ dup key: { : \"001\" }" } ,
{ "index" : 1, "code" : 11000, "errmsg" : "E11000 duplicate key error collection: traffic.redirects index: _id_ dup key: { : \"002\" }" } , { "index" : 2, "code" : 11000, "errmsg" : "E11000 duplicate key error collection: traffic.redirects index: _id_ dup key: { : \"003\" }" } ]}
I would then take those 3 documents and do bulk_write with replace_one.
all of this is working well until I get the catch block described above that logs me a raw_server_error() document that is empty :
{ }
Then bwex.what()gives me the message document to insert contains invalid keys: generic server error
and bwex.code() gives me mongodb:22
What's the problem here ? my documents are no different then when it works good. and if I retry it can work after.
(I've maximized my collection of bulk insert to one document to be sure of that).
Thanks for the help.
(using mongo-c-driver-1.6.0, mongo-cxx-driver-r3.1.1 and mongod 3.4.2)