簡體   English   中英

如何使用 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++ 異常:mongocxx::v_noabi::bulk_write_exception at memory location 0x000000B26C12DF30。 這是我的源代碼

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());拋出了一個異常collection.insert_one(doc.view()); . 您應該捕獲該異常(通過使用trycatch ),並檢查異常的內容,這應該會告訴您更多關於發生了什么問題的信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM