mongocxx driver storing BSON Objects

150 views
Skip to first unread message

Alberto Seco

unread,
Jan 24, 2017, 5:14:14 AM1/24/17
to mongodb-user
Hi all,

I'm developing an application that needs to store in std::map and std::vector bson objects.
Reading the manual http://mongodb.github.io/mongo-cxx-driver/mongocxx-v3/working-with-bson/, it is written that not use view, because of the the possible dangling pointer. So I'm trying to use bsoncxx:document::value, but when I want to access an element of a map, then the compiler says that there is no default constructor. 
So which is the best way to store in the maps and vector the bson objects?

The error compiling it is:

g++ -Ofast   -c -O3 -DCONSOLE_APP -I/usr/local/include/mongocxx/v_noabi -I/usr/local/include/bsoncxx/v_noabi -I/usr/local/include/libmongoc-1.0 -I/usr/local/include/libbson-1.0 -std=c++11 -MMD -MP -MF "build/Release/GNU-MacOSX/_ext/1719473742/ccachedata.o.d" -o build/Release/GNU-MacOSX/_ext/1719473742/ccachedata.o ../searchlib/ccachedata.cpp
In file included from ../searchlib/ccachedata.cpp:8:
In file included from ../searchlib/ccachedata.h:12:
In file included from /usr/local/include/boost/thread.hpp:13:
In file included from /usr/local/include/boost/thread/thread.hpp:12:
In file included from /usr/local/include/boost/thread/thread_only.hpp:17:
In file included from /usr/local/include/boost/thread/pthread/thread_data.hpp:10:
In file included from /usr/local/include/boost/thread/exceptions.hpp:20:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/string:439:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:628:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:1740:31: error: no matching constructor for initialization of 'bsoncxx::v_noabi::document::value'
            ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
                              ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:1656:18: note: in instantiation of function template specialization 'std::__1::allocator<std::__1::__tree_node<std::__1::__value_type<bsoncxx::v_noabi::oid, bsoncxx::v_noabi::document::value>, void *> >::construct<bsoncxx::v_noabi::document::value>' requested here
            {__a.construct(__p, _VSTD::forward<_Args>(__args)...);}
                 ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:1502:14: note: in instantiation of function template specialization 'std::__1::allocator_traits<std::__1::allocator<std::__1::__tree_node<std::__1::__value_type<bsoncxx::v_noabi::oid, bsoncxx::v_noabi::document::value>, void *> > >::__construct<bsoncxx::v_noabi::document::value>' requested here
            {__construct(__has_construct<allocator_type, _Tp*, _Args...>(),
             ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/map:1495:20: note: in instantiation of function template specialization 'std::__1::allocator_traits<std::__1::allocator<std::__1::__tree_node<std::__1::__value_type<bsoncxx::v_noabi::oid, bsoncxx::v_noabi::document::value>, void *> > >::construct<bsoncxx::v_noabi::document::value>' requested here
    __node_traits::construct(__na, _VSTD::addressof(__h->__value_.__cc.second));
                   ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/map:1561:29: note: in instantiation of member function 'std::__1::map<bsoncxx::v_noabi::oid, bsoncxx::v_noabi::document::value, std::__1::less<bsoncxx::v_noabi::oid>, std::__1::allocator<std::__1::pair<const bsoncxx::v_noabi::oid, bsoncxx::v_noabi::document::value> > >::__construct_node_with_key' requested here
        __node_holder __h = __construct_node_with_key(_VSTD::move(__k));
                            ^
../searchlib/ccachedata.cpp:273:68: note: in instantiation of member function 'std::__1::map<bsoncxx::v_noabi::oid, bsoncxx::v_noabi::document::value, std::__1::less<bsoncxx::v_noabi::oid>, std::__1::allocator<std::__1::pair<const bsoncxx::v_noabi::oid, bsoncxx::v_noabi::document::value> > >::operator[]' requested here
                                kwProductCategoriesAux[pc_ele.get_oid().value].push_back(allKwAux[pc_ele.get_oid().value]);
                                                                                               ^
/usr/local/include/bsoncxx/v_noabi/bsoncxx/document/value.hpp:71:14: note: candidate constructor not viable: requires single argument 'view', but no arguments were provided
    explicit value(document::view view);
             ^
/usr/local/include/bsoncxx/v_noabi/bsoncxx/document/value.hpp:73:5: note: candidate constructor not viable: requires 1 argument, but 0 were provided
    value(const value&);
    ^
/usr/local/include/bsoncxx/v_noabi/bsoncxx/document/value.hpp:76:5: note: candidate constructor not viable: requires 1 argument, but 0 were provided
    value(value&&) noexcept = default;
    ^
/usr/local/include/bsoncxx/v_noabi/bsoncxx/document/value.hpp:61:5: note: candidate constructor not viable: requires 2 arguments, but 0 were provided
    value(unique_ptr_type ptr, std::size_t length);
    ^
/usr/local/include/bsoncxx/v_noabi/bsoncxx/document/value.hpp:50:5: note: candidate constructor not viable: requires 3 arguments, but 0 were provided
    value(std::uint8_t* data, std::size_t length, deleter_type dtor);
    ^
1 error generated.

David Golden

unread,
Jan 24, 2017, 1:55:55 PM1/24/17
to mongodb-user
Hi, Alberto.

It would help if you could post an SSCCE so we could understand exactly what you're doing.

That said, what you might need to do is std:move() the bsoncxx::document::value objects into a vector of them (to keep them alive), and then have your map be bsoncxx::document::views derived from those document::values.

Regards,
David

Alberto Seco

unread,
Jan 25, 2017, 8:13:55 AM1/25/17
to mongodb-user
Hi David,

Thank you for answering.

So the best way is to store in a vector the bsoncxx:document:value moving it, and then in maps and other structures use the bsoncxx:document::view. 

Also I tried to use the view_or_value, storing the value and it seems that it works so.

I will try it, I'm recoding a c++ software that uses the legacy driver for using the last mongocxx driver.

Best regards
Alberto

David Golden

unread,
Jan 25, 2017, 8:58:57 AM1/25/17
to mongodb-user
view_or_value does the same sort of thing behind the scenes, keeping the value alive while providing a view to it.  My approach keeps things more explicit, but if you prefer view_or_value, that's fine, too.

David
Reply all
Reply to author
Forward
0 new messages