|
Working on the following collection:
{"_id":{"$oid":"58e17a4dd7b07d1fee077fac"},"item":"journal","instock":[{"warehouse":"A","qty":5.0},{"warehouse":"C","qty":15.0}]}
|
{"_id":{"$oid":"58e17a4dd7b07d1fee077fad"},"item":"notebook","instock":[{"warehouse":"C","qty":5.0}]}
|
{"_id":{"$oid":"58e17a4dd7b07d1fee077fae"},"item":"paper","instock":[{"warehouse":"A","qty":60.0},{"warehouse":"B","qty":15.0}]}
|
{"_id":{"$oid":"58e17a4dd7b07d1fee077faf"},"item":"planner","instock":[{"warehouse":"A","qty":40.0},{"warehouse":"B","qty":5.0}]}
|
{"_id":{"$oid":"58e17a4dd7b07d1fee077fb0"},"item":"postcard","instock":[{"warehouse":"B","qty":15.0},{"warehouse":"C","qty":35.0}]}
|
When finding the "instock" item by:
bson_iter_find_descendant(&Iter, "instock", &Desc))
|
it is correctly of type ARRAY and can be printed with:
if (BSON_ITER_HOLDS_ARRAY(&Iter)) {
|
char *str = NULL;
|
bson_t *arr;
|
const uint8_t *data = NULL;
|
uint32_t len = 0;
|
|
bson_iter_array(&Desc, &len, &data);
|
arr = bson_new_from_data(data, len);
|
str = bson_as_json(arr, NULL);
|
bson_free(str);
|
bson_destroy(arr);
|
str is:
{"0":{"warehouse":"A","qty":5.0},"1":{"warehouse":"C","qty":15.0}}
|
{"0":{"warehouse":"C","qty":5.0}}
|
{"0":{"warehouse":"A","qty":60.0},"1":{"warehouse":"B","qty":15.0}}
|
{"0":{"warehouse":"A","qty":40.0},"1":{"warehouse":"B","qty":5.0}}
|
{"0":{"warehouse":"B","qty":15.0},"1":{"warehouse":"C","qty":35.0}}
|
It is also possible to retrieve the values of wareahouse or qty by something like:
bson_iter_find_descendant(&Iter, "instock.0.warehouse", &Desc))
|
or
|
bson_iter_find_descendant(&Iter, "instock.1.qty", &Desc))
|
However when using:
bson_iter_find_descendant(&Iter, "instock.0", &Desc))
|
One would expect to retrieve an item of type DOCUMENT. But instead it is retrieved as type ARRAY and when trying to execute:
bson_iter_document(&Desc, &len, &data);
|
arr = bson_new_from_data(data, len);
|
data is null and the program crashes.
|