Bsoncxx: check if element exists

Hello everyone,

I’m using the following code to get a string from a bsoncxx::document::value object (called data here):

auto view = data.view();

std::string field_value;
try
{
    field_value = view["field_name"].get_utf8().value.to_string();
}
catch (const bsoncxx::exception &e)
{
    //field_name does not exist
}

On machine A an exception is thrown as expected if view does not contain “field_name”. However, on machine B a SIGABRT is raised and the program crashes. Is this not the proper way to handle a missing field?

Thank you.

1 Like