2013-12-17 35 views
0
mongo_cursor *cursor=mongo_find(conn,TEST_NS,query,NULL,0,0,0); 

    count_matched=0;  
    bson *doc; 

    while(mongo_cursor_next(cursor)==MONGO_OK) 
    { 
     count_matched++; 

     doc=(bson *)mongo_cursor_bson(cursor); 
     bson_iterator_init(&it,doc); 

    while(bson_iterator_next(&it) != BSON_EOO) 
    { 
     fprintf(stderr,"%s : %s\n\n",bson_iterator_key(&it),bson_iterator_string(&it)); 

    } 
    } 

該代碼可以正常使用,我可以看到匹配的文件(鍵+值),但現在我想匹配的文檔的鍵和值保存到一個字符串。任何可以告訴我如何我可以將鍵和值的返回值保存到一個字符串?查詢的MongoDB鍵和值使用C驅動

一號文件包括(所有字符串)

Total Key=10 
Total value=10 

,我想一次保存10號文件的鍵和值。我正在使用mongodb的C驅動程序。

+0

你可以使用'sprintf'嗎? – WiredPrairie

+0

我在我的答案下面添加了,你能否請檢查一下,讓我知道它的有效性..在我的環境中正常工作..或者如果你可以提供一些其他的建議來利用它。謝謝 –

回答

0
mongo_cursor *cursor=mongo_find(conn,TEST_NS,query,NULL,0,0,0); 

    count_matched=0;  
    bson *doc; 

    //Answer 
    const char* temp_key[100][100],temp_value[100][100]; 
    int i=0; 
    while(mongo_cursor_next(cursor)==MONGO_OK) 
    { 
     count_matched++; 

     doc=(bson *)mongo_cursor_bson(cursor); 
     bson_iterator_init(&it,doc); 

    while(bson_iterator_next(&it) != BSON_EOO) 
    { 
     fprintf(stderr,"%s : %s\n\n",bson_iterator_key(&it),bson_iterator_string(&it)); 
     temp[i][0]=bson_iterator_key[&it]; //Answer 
     temp_value[i][0]=bson_iterator_key[&it]; //Answer 
     i++; //Answer 
    } 

    } 

只是爲了記錄在案,這是草圖,我知道關於臨時變量的腐敗及其溢出,但我會根據我的代碼刪除。

1

以下代碼顯示如何將鍵和值從bson迭代器複製到鍵值數組temp_key和temp_value中。特定的代碼塊位於標記爲START和END的註釋之間。

此外,您可以在http://api.mongodb.org/c/current/bson.html找到訪問BSON文檔內容的文檔。

mongo_cursor *cursor = mongo_find(&conn, TEST_NS, &query, NULL, 0, 0, 0); 
int count_matched = 0; 
bson *doc; 

// Assuming you are just looking for 100 key/value pair of max length of 99 characters 
const unsigned KV_ARRAY_LENGTH = 100; 
const unsigned MAX_KV_LENGTH = 105; 
char temp_key[KV_ARRAY_LENGTH][MAX_KV_LENGTH + 1], temp_value[KV_ARRAY_LENGTH][MAX_KV_LENGTH + 1]; 
int i = 0; 
while (mongo_cursor_next(cursor) == MONGO_OK) { 
    count_matched++; 
    doc=(bson *)mongo_cursor_bson(cursor); 

    bson_iterator it; 
    bson_iterator_init(&it,doc); 

    while (bson_iterator_next(&it) != BSON_EOO) { 
     fprintf(stderr,"%s : %s\n", bson_iterator_key(&it), bson_iterator_string(&it)); 

     /******* START - Code to capture key-value into appropriate array */ 
     if (i < KV_ARRAY_LENGTH) { 
      /* - Collect key-value pairs only if there is space in the array 
      * - Key/Value woud be captured only till the max amount of space available for them i.e. MAX_KV_LENGTH in this case 
      * */ 
      strncpy(temp_key[i], bson_iterator_key(&it), MAX_KV_LENGTH); 
      strncpy(temp_value[i], bson_iterator_string(&it), MAX_KV_LENGTH); 
      temp_key[i][MAX_KV_LENGTH] = temp_value[i][MAX_KV_LENGTH] = '\0'; 
      ++i; 
     } else { 
      /* whatever need to be done if there is no room in the array */ 
     } 
     /******* END - Code to capture key-value into appropriate array */ 
    } 
} 

/* Test iterating through the key-value pair constructed in query iteration */ 
fprintf(stdout, "--- Fields collected ---\n"); 
int keyIndex = 0; 
for (; keyIndex < i; ++keyIndex) { 
    fprintf(stdout, "{key: %s, value: %s}\n", temp_key[keyIndex], temp_value[keyIndex]); 
}