2

Im trying to get something done with C++ and MongoDB. So far there has been myriad of problems, but I have pulled through.

Then I got this one:

terminate called after throwing an instance of 'mongocxx::v_noabi::logic_error'
  what():  invalid use of default constructed or moved-from mongocxx::client object
Aborted

And frankly, Im losing hope. This is the example Im trying to run: https://docs.mongodb.com/getting-started/cpp/insert/.

Error appears when I try to run the compiled program. Im able to compile and run the 'hellomongo' example just fine, so at least partly, the driver is installed correctly.

My code:

#include <chrono>

#include <bsoncxx/builder/stream/document.hpp>
#include <bsoncxx/types.hpp>

#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
#include <mongocxx/uri.hpp>

using bsoncxx::builder::stream::document;
using bsoncxx::builder::stream::open_document;
using bsoncxx::builder::stream::close_document;
using bsoncxx::builder::stream::open_array;
using bsoncxx::builder::stream::close_array;
using bsoncxx::builder::stream::finalize;

int main(int, char**)   
{

    mongocxx::instance inst{};
    mongocxx::client conn{};

    auto db = conn["test"];

    bsoncxx::document::value restaurant_doc =
        document{} << "address" << open_document << "street"
                   << "2 Avenue"
                   << "zipcode"
                   << "10075"
                   << "building"
                   << "1480"
                   << "coord" << open_array << -73.9557413 << 40.7720266 << close_array
                   << close_document << "borough"
                   << "Manhattan"
                   << "cuisine"
                   << "Italian"
                   << "grades" << open_array << open_document << "date"
                   << bsoncxx::types::b_date { std::chrono::system_clock::time_point {
    std::chrono::milliseconds { 12323 } } } << "grade"
                   << "A"
                   << "score" << 11 << close_document << open_document << "date"
                   << bsoncxx::types::b_date { std::chrono::system_clock::time_point {
    std::chrono::milliseconds { 12323 } } } << "grade"
                   << "B"
                   << "score" << 17 << close_document << close_array << "name"
                   << "Vella"
                   << "restaurant_id"
                   << "41704620" << finalize;

    // We choose to move in our document here, which transfers ownership to insert_one()
    auto res = db["restaurants"].insert_one(std::move(restaurant_doc));
}

I use the following command to compile the example:

c++ --std=c++11 test.cpp -o test $(pkg-config --cflags --libs libmongocxx)

Any help is appreciated! I have very little experience with C++, so I'm bit lost to what could be the problem.

acm
  • 12,183
  • 5
  • 39
  • 68
Ankk4
  • 31
  • 1
  • 6
  • Please read the comments for a default constructed mongocxx::client: http://mongodb.github.io/mongo-cxx-driver/classmongocxx_1_1client.html#a4fe390cb757d5e594b56e98348038ccf You need to call the constructor that takes a mongocxx::uri to actually connect the client: http://mongodb.github.io/mongo-cxx-driver/classmongocxx_1_1client.html#a4a1f479b088cf658ddfa16f6bb553b95 – acm Aug 09 '16 at 13:05
  • Also, I've alerted the documentation team that the docs on docs.mongodb.com are out of date. In general, the documentation linked from the github page is more up to date, and the source code even more so. In particular, the example that you are citing derives from the code in the examples directory (https://github.com/mongodb/mongo-cxx-driver/tree/master/examples) which are automatically build and run as part of the CI system for the C++11 driver, so they should always be correct. – acm Aug 09 '16 at 13:14
  • Thank you! I can finally get some work done. – Ankk4 Aug 11 '16 at 09:25

1 Answers1

1

As acm pointed out, the docs on docs.mongodb.com are out of date. Github examples are working fine. I will mark this as answered.

Ankk4
  • 31
  • 1
  • 6