Search code examples
c++bsonmongo-cxx-driver

bsoncxx: document::view vs document::value


Please explain the difference between bsoncxx::document::value and bsoncxx::document::view. Is view just a proxy to value class?


Solution

  • In the bsoncxx library, values represent ownership of an immutable resource, but not the ability to inspect the owned resource. To inspect the owned resource, you obtain a view from the value. The view and value here are analogous to the relationship between std::string and std::string_view. By separating the aspects of ownership from inspection, we can have a cheap type used for APIs that only need to look at data, and a more expensive API for when we need to make copies or take over ownership of resources.