BSONObj bsonObj = BSON("doubleVal" << 3.14159); BSONElement element = bsonObj.getField("doubleVal"); double value = element.Double();
BSONObjBuilder builder; builder.append("intVal", 42); BSONObj bsonObj = builder.obj();In this example, we create a BSON document using a BSONObjBuilder and set an integer value using the append() method. We then build the BSON object using the obj() method. The BSONElement Number class is part of the MongoDB C++ Driver package library.