Question about (*client)[databaseName][collectionName].bulk_write and mongocxx::bulk_write_exception

169 views
Skip to first unread message

Vin

unread,
Feb 27, 2017, 12:19:19 PM2/27/17
to mongodb-user
Hi,
I'm having a hardtime figuring out what's going on with my code and how to use cmongocxx driver objects :


i'm doing the following :

std::vector<mongocxx::model::write> writes;
mongocxx::options::bulk_write bulkWriteOption;
bulkWriteOption.ordered(false);
//...
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)

Vin

unread,
Feb 28, 2017, 11:45:48 AM2/28/17
to mongodb-user
Well,
in fact I was wrong, my problem is reproducable, and is not because of an cmongocxx bug as I thought first.  

My problem comes from the fact that incoming data that I tried to store is validated as JSON data but mongoDB doesn't accept JSON but BSON which is a subset of JSON.

It woudl be great if the  mongocxx::bulk_write_exception could give a detailed error as when we try to insert a collection of documents and some of them have a duplicate _id

David Golden

unread,
Mar 3, 2017, 1:29:42 PM3/3/17
to mongodb-user
Hi, Vin.  BSON supports a superset of JSON types, but BSON is a binary format whereas JSON is a text format.

If you are receiving JSON, you should can use the "bsoncxx::from_json" function to convert.

Regards,
David
Reply all
Reply to author
Forward
0 new messages