2

C++ と MongoDB で何かを成し遂げようとしています。今までいろいろな問題がありましたが、乗り越えてきました。

それから私はこれを手に入れました:

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

そして率直に言って、私は希望を失っています。これは、私が実行しようとしている例です: https://docs.mongodb.com/getting-started/cpp/insert/

コンパイルしたプログラムを実行しようとすると、エラーが表示されます。「hellomongo」の例を問題なくコンパイルして実行できるので、少なくとも部分的にはドライバーが正しくインストールされています。

私のコード:

#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));
}

次のコマンドを使用して例をコンパイルします。

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

どんな助けでも大歓迎です!私は C++ の経験がほとんどないので、何が問題なのか少しわかりません。

4

1 に答える 1

1

acm が指摘したように、docs.mongodb.com のドキュメントは古くなっています。Github の例は正常に動作しています。これを回答済みとしてマークします。

于 2016-08-11T09:22:56.733 に答える