使用 monogodb 的C API 來遍歷 bson 的數組對象

使用 monogodb 的C API 來遍歷 bson 的數組對象 數組

   使用 bson_printf 打印下數組對象 相似於這樣 app

    ips : 4
            0 : 3
                    ip : 2   127.0.0.1:2235

            1 : 3
                    ip : 2   127.0.0.1:9666 socket

一共有3層  第一層是 object 的名字 遍歷的時候使用 bson_find 使用名字查找到 object 後 使用 bson_iterator_subobject 進入這個object 也就是進入 第二層 觀察 第二次 能夠發現 這個 object 的名字是字符的 0 1 2 3 ,,,也就是數組的索引  因此之後 循環構造這樣的字符串 查找每一項 找到一項 就使用 bson_iterator_subobject 進入最後一層 也就是實際的數據了。。。 url

具體 能夠參考下面的代碼 spa

01 int main()
02 {
03     mongo conn[1];
04     mongo_cursor* cursor;
05  
06     mongo_init_sockets();
07     //Parameters (connection, IP, port);
08     int status = mongo_client(conn, "127.0.0.1", 27017);
09     if(status != MONGO_OK){
10         switch(conn->err){
11         case MONGO_CONN_NO_SOCKET: printf("Socket not found\n"); return 1;
12         case MONGO_CONN_FAIL: printf("Connection Failed\n"); return 1;
13         case MONGO_CONN_NOT_MASTER: printf("Not master\n"); return 1;
14         }
15     }
16     bson query[1];
17     bson_init(query);
18     bson_append_string(query,"md5","97296BE70AC48609704936C5BDAF0312");
19     bson_finish(query);
20  
21     bson fields[1];
22     bson_init(fields);
23     bson_append_bool(fields,"md5",1);
24     bson_append_bool(fields,"ips",1);
25     bson_append_bool(fields,"urls",1);
26     bson_finish(fields);
27  
28     cursor = mongo_find(conn,"test.virus", query, fields, 9999, 0, 0);
29  
30     bson_destroy(query);
31     bson_destroy(fields);
32  
33     while(mongo_cursor_next(cursor) == MONGO_OK)
34     {
35         bson_print(mongo_cursor_bson( cursor ));
36         bson_iterator it;
37         if (bson_find(&it, &cursor->current, "ips"))
38         {
39             bson sub;
40             bson_iterator sub_it;
41             int idx = 0;
42             char key[32]={'0',0};
43             _itoa(idx,key,10);
44             bson_iterator_subobject( &it, &sub );
45             bson_print(&sub);
46             while(bson_find(&sub_it,&sub,key))
47             {
48                 bson arr;
49                 bson_type t;
50                 bson_iterator bi;
51                 bson_iterator_subobject( &sub_it, &arr);
52                 bson_iterator_init(&bi,&arr);
53                 while( (t = bson_iterator_next(&bi)))
54                 {
55                     printf("bson type : %d \n",t);
56                     switch(t)
57                     {
58                     case BSON_OBJECT:
59                     case BSON_ARRAY:
60                         {
61  
62                         }
63                         break;
64                     case BSON_STRING:
65                         printf("%s \n",bson_iterator_string(&bi));
66                         break;
67                     default:
68                         break;
69                     }
70                     printf("%s  \n",bson_iterator_key(&bi));
71                 }
72                 idx++;
73                 _itoa(idx,key,10);
74             }
75         }
76     }
77     mongo_cursor_destroy(cursor);
78     mongo_destroy(conn);
79     return 0;
80 }
相關文章
相關標籤/搜索