使用 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 }
相关文章
相关标签/搜索