如何使用 mongo-cxx-driver 的 **insert** 函数将包含静态数据的多维数组插入到数据库中
How to inserta nd retreive Multidimension array with static data in it into database using **insert** function of mongo-cxx-driver?
我尝试了将数组传递给包装器函数的传统方法,在该函数中,我使用 insertOne 使用 for 循环插入数据。没有构建问题,但在运行时,我遇到了此错误:Microsoft C++异常:内存位置 0x000000B26C12DF30 的 mongocxx::v_noabi::bulk_write_exception。这是我的源代码。
int main(void) {
char EUI[][20] = { "10205E3710014240", "10205e37100142cc" ,"10205E6910001E58", "10205E371001426C" };
char IP[][15] = { "192.168.85.117" , "192.168.85.114", "192.168.85.186", "192.168.85.168" };
int i = 4;
push_data(IP, EUI, i);
while (1);
}
void push_data(char IP[][15], char EUI[][20], int count)
{
mongocxx::instance inst{};
mongocxx::client conn{ mongocxx::uri{} };
auto collection = conn["new"]["collection"];
int a;
builder::stream::document builder{};
auto in_array = builder << "subdocs" << builder::stream::open_array;
for (a = 0; a<count; a++) {
in_array = in_array << builder::stream::open_document << EUI[a] << IP[a]
<< builder::stream::close_document;
}
auto after_array = in_array << builder::stream::close_array;
bsoncxx::document::value doc = after_array << builder::stream::finalize;
bsoncxx::document::view view = doc.view();
for (a = 0; a < count; a++) {
collection.insert_one(doc.view());
}
auto cursor = collection.find({});
for (auto&& doc : cursor) {
std::cout << bsoncxx::to_json(doc) << std::endl;
}
}
几乎可以肯定,collection.insert_one(doc.view());
中抛出了异常。您应该捕获该异常(通过使用 try
和 catch
),并检查异常的内容,这应该会告诉您有关出错的更多信息。
相关文章:
- C++json插入数组
- 正在插入动态数组
- 如何在向量数组中插入元素?
- 将值插入到标准 c++ 数组的正确索引(升序)中
- C++ - 将元素按升序插入数组的 SEG 错误
- 将二维数组的所有元素插入到一维数组中
- 插入数组时违反写访问冲突
- 尝试将对象插入空指针数组时出现分段错误
- 类数组插入函数
- 将数组的元素插入映射的时间复杂度是多少?
- 当索引位于末尾和空数组时,向数组插入元素时出现问题
- C++ 使用数组插入到二叉搜索树中
- 将 2D 数组插入 std::vector 时"cannot convert from 'const GLfloat [12]' to '_Objty'"错误消息
- 如何使用 mongo-cxx-driver 的 **insert** 函数将包含静态数据的多维数组插入到数据库中
- 如何将字符数组插入浮点向量
- 如何将数组插入无序集合
- 用c++将数组数组插入mongo中
- visual C++将整数的二维数组插入到另一个整数的二维阵列中
- 优化数组插入C/ c++
- c++如何将数组插入哈希集