如何在 C++ 中使用 MongoDB 的 insert_many() 方法?
How to use insert_many() method of MongoDB in C++?
我正在使用一种方法将一些数据存储在 MongoDB 数据库中。
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);
}
我知道该列表存储了 class_a
的 1 个以上对象。我使用mongocxx::collection
对象collection
的方法name()
来测试它是否可以访问。它按预期返回了它的名字。所以我认为有一个客户群。但是 insert_many()
方法抛出错误:
"mongoc_bulk_operation_execute() requires a client and one has not been set.: generic server error"
我做错了什么?
我明白了,函数 acquire() 来自指针 std::unique_ptr _pool returns 客户端。
我正在使用一种方法将一些数据存储在 MongoDB 数据库中。
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);
}
我知道该列表存储了 class_a
的 1 个以上对象。我使用mongocxx::collection
对象collection
的方法name()
来测试它是否可以访问。它按预期返回了它的名字。所以我认为有一个客户群。但是 insert_many()
方法抛出错误:
"mongoc_bulk_operation_execute() requires a client and one has not been set.: generic server error"
我做错了什么?
我明白了,函数 acquire() 来自指针 std::unique_ptr _pool returns 客户端。