BSONObj obj = BSONObjBuilder() .append("name", "John") .append("age", 30) .obj();
if (obj.hasField("name")) { std::string name = obj.getStringField("name"); std::cout << "Name: " << name << std::endl; }In this example, the getStringField method is used to get the value of the "name" field in the BSONObj. If the field exists, its value is assigned to the variable "name" and printed to the console. The BSONObj class is part of the MongoDB C++ driver, which is a package library for interacting with MongoDB databases using C++. It provides a rich set of features for working with BSON documents, such as serialization/deserialization, query building, and aggregation.