2

I am using a method to store some data in a MongoDB database.

void save_data(std::vector< class_a > list){
    using namespace std;
    using bsoncxx::builder::stream::document;
    using bsoncxx::builder::stream::finalize;

    std::vector< bsoncxx::document::value > documents;
    for (size_t i = 0; i < list.size(); i++){
        documents.push_back(document{}
            << "field 1" << list[i].get_data_1()
            << "field 2" << list[i].get_data_2()
            << finalize);
    }

    collection.insert_many(documents);
}

I know the list has stored more than 1 object of class_a. I used the method name() of the mongocxx::collection object collection to test if it is accessable. It returned its name as expected. So there is a client set, I think. But the insert_many() method throws an error:

"mongoc_bulk_operation_execute() requires a client and one has not been set.: generic server error"

What am I doing wrong?

Shinji Ikari
  • 173
  • 1
  • 12
  • As written, your code will not compile. Where does ```collection``` come from? My first guess as to why it doesn't work is that you have not kept a ```mongocxx::client``` object alive. The lifetime of a ```collection``` object must be a subset of the lifetime of the ```client``` object that created it. – acm Feb 19 '17 at 00:11
  • `collection` is an `mongocxx::collection` object. It is alive because of the singleton pattern ([look here](https://github.com/mongodb/mongo-cxx-driver/blob/releases/stable/examples/mongocxx/instance_management.cpp)). I tested it by printing the collections name with `collection.name()`. – Shinji Ikari Feb 19 '17 at 07:48
  • I'm the author of that example. That singleton keeps a ```mongocxx::instance``` object alive. But it doesn't keep a ```mongocxx::client``` alive. I really suggest you show a complete working example of your code - it will make it much easier to figure out what is going wrong. – acm Feb 19 '17 at 14:48
  • Here you can find the [main.cpp](https://drive.google.com/open?id=0B52JZDzktVOfbzEyVWx0LWM0cjQ), the [data_access.hpp](https://drive.google.com/open?id=0B52JZDzktVOfNmJKMHI1SEZKQms) and the [data_access.cpp](https://drive.google.com/open?id=0B52JZDzktVOfZ2FTYThnMU9FRFE). – Shinji Ikari Feb 19 '17 at 17:14
  • In the future, please post the code inline in your question, but I'll point out that your ```data_access::data_access``` constructor never initializes the ```data_access::_collection``` member, but uses that member in ```data_acess:save_data```. – acm Feb 19 '17 at 23:14
  • Is there a way to keep `mongocxx::client` alive the same way as `mongocxx::instance`? – Shinji Ikari Feb 20 '17 at 09:24
  • No, you must manage its lifetime in a way that is appropriate to your application. You should also consider using ```mongocxx::pool``` rather than individual instances of ```mongocxx::client```. However, the same rules apply regarding lifetime. – acm Feb 20 '17 at 15:41

1 Answers1

0

I see, the function acquire() from the pointer std::unique_ptr _pool returns the client.

Shinji Ikari
  • 173
  • 1
  • 12