I am trying to add document inside docker container with cpp driver. The container crashes without any error msg. I dont have any kind of error to know whats going on.
Here is the sample code.
void handle_request(const Request& req, Response& res, mongocxx::database &db) {
if (req.has_header("Content-Type") && req.get_header_value("Content-Type") == "application/json") {
const auto& json_data = req.body;
// std::cout << "Received JSON dataaa: " << json_data << std::endl;
std::cout << "DEBUG"<<std::endl;
auto document = bsoncxx::from_json(req.body);
std::cout << "DEBUG"<< std::endl;
cout<<"Get collection"<<endl;
auto collection = db["injester"]; // Replace with your actual collection name
bsoncxx::document::value doc_value = bsoncxx::builder::basic::make_document(bsoncxx::builder::basic::kvp("i", 0));
std::cout << "Inserting document: " << bsoncxx::to_json(doc_value.view()) << std::endl;
try {
collection.insert_one(doc_value.view());
} catch (const mongocxx::exception& e) {
std::cerr << "MongoDB Exception: " << e.what() << std::endl;
} catch (const std::exception& e) {
std::cerr << "Standard Exception: " << e.what() << std::endl;
} catch (...) {
std::cerr << "Unknown Exception" << std::endl;
}
// collection.insert_one(document.view());
res.set_content("JSON data added to MongoDB", "text/plain");
// res.set_content("JSON data received successfully", "text/plain");
} else {
res.status = 400;
res.set_content("Bad Request: Expecting JSON data", "text/plain");
}
}