迭代bson数组子对象

时间:2015-07-08 07:39:29

标签: c bson

我想迭代bson对象中的drmData值:

var loadedBson = {
    // some fields
    drmData : [
        [0.1,3.1415],
        [0.2,3.1417],
        [0.3,3.1418],
        [0.4,3.1419]
    ]
}

这是包含2个版本的代码:

bson *loadedBson = ejdbloadbson(coll, &_oid);

bson_iterator it;
bson_type bt;

bt = bson_find(&it, loadedBson, "drm_data");
if (bt != BSON_ARRAY)
    error();

bson_iterator it_sub;
bson_iterator_subiterator(&it, &it_sub);  // go inside "loadedBson.drmData"

while (bson_iterator_more(&it_sub)) {
    bt = bson_iterator_next(&it_sub);
    if (bt != BSON_OBJECT)
        continue;

    bson_iterator it_sub_2;
    bson_iterator_subiterator(&it_sub, &it_sub_2);

    // version 1 started - it does not work
    bt = bson_find(&it_sub_2, loadedBson, "val_i");
    if (bt != BSON_INT)                           // GDB: bt == BSON_EOO - bad
         continue;
    int val_i = bson_iterator_int(&it_sub_2);

    bt = bson_find(&it_sub_2, loadedBson, "val_t");
    if (bt != BSON_INT)
        continue;
    int val_t = bson_iterator_int(&it_sub_2);

    printf("%d %d\n", val_i, val_t);
    // version 1 ended


    // version 2 started - it works
    bt = bson_iterator_next(&it_sub_2);
    if (bt != BSON_INT)                          // GDB: bt == BSON_INT - good
        continue;
    int val_i = bson_iterator_int(&it_sub_2);

    bt = bson_iterator_next(&it_sub_2);
    if (bt != BSON_INT)
        continue;
    int val_t = bson_iterator_int(&it_sub_2);

    printf("%d %d\n", val_i, val_t);
    // version 2 ended
}    

我查看gdb,看看我何时使用bt = bson_find(&it_sub_2, loadedBson, "val_i");这不起作用并返回BSON_EOO,因此,当我将迭代器简单地移动到下一个bt = bson_iterator_next(&it_sub_2);时 - 它可以工作。

为什么第一个版本不起作用?

0 个答案:

没有答案